-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (75 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {PubSub} from 'graphql-subscriptions';
import {createServer} from 'http';
import {createServer as createHTTPSServer} from "https";
import {SubscriptionServer} from 'subscriptions-transport-ws';
import {execute, subscribe} from 'graphql';
import {readFileSync} from "fs";
import {makeExecutableSchema} from 'graphql-tools';
import resolvers from "./resolvers";
import {setupDB} from "./db";
import {loadMonstersFromFile} from "./entities/monster";
import {validateAuthID} from "./entities/players";
import * as fs from "fs";
require("dotenv").config();
export const pubsub = new PubSub();
const WS_PORT = 5000;
const typeDefs = readFileSync("./schema.graphql", "utf-8");
// does not work due to performance issues
// loadMonstes().then(() => console.log(monsters.slice(10))).catch(console.error);
loadMonstersFromFile()
export let db;
async function setup() {
let a = (req, res) => {
if (req.method === 'POST' && req.url === "/cli/upload") {
let body = ''
req.on('data', (data) => body += data);
req.on('end', () => {
console.log('Body: ' + body)
res.writeHead(200)
res.end('post received')
})
} else {
res.writeHead(501);
res.end();
}
}
let websocketServer = {};
if (process.env.HTTPS_KEY_PATH && process.env.HTTPS_CERT_PATH)
websocketServer = createHTTPSServer({
key: fs.readFileSync(process.env.HTTPS_KEY_PATH),
cert: fs.readFileSync(process.env.HTTPS_CERT_PATH)
}, a)
else {
websocketServer = createServer(a);
}
websocketServer.listen(WS_PORT, () => console.log(
`Websocket Server is now running on port ${WS_PORT}`
));
db = await setupDB();
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
SubscriptionServer.create(
{
schema,
execute,
subscribe,
onConnect: (connectionParams) => {
return validateAuthID(connectionParams.authID)
.then(user => {
return {
currentUser: user,
};
});
}
},
{
server: websocketServer,
path: '/graphql',
},
);
}
setup();
process.on('SIGINT', () => db.close());
process.on('SIGTERM', () => db.close());