forked from chrisinajar/proofofcombat-server
/
index.ts
176 lines (150 loc) Β· 4.24 KB
/
index.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import "dotenv/config";
import { ApolloServer } from "apollo-server-express";
import { hiveApollo } from "@graphql-hive/client";
import { ApolloServerPluginDrainHttpServer } from "apollo-server-core";
import express from "express";
import fs from "fs";
import http from "http";
import https from "https";
import cors from "cors";
import schema from "./schema";
import type { BaseContext } from "./schema/context";
import db from "./db";
import { confirm } from "./security";
import { addSocketToServer } from "./socket";
const port = process.env.HTTP_PORT ?? 8880;
const httpsPort = process.env.HTTPS_PORT ?? 8443;
const socketIoPort = process.env.SOCKET_PORT ?? 2096;
const corsOptions = {
origin: true,
credentials: true,
allowedHeaders: [
"Authorization",
"Content-Type",
"ApolloGraphQL-Client-Name",
"ApolloGraphQL-Client-Version",
],
};
function getHttpsServer(app?: express.Application): http.Server {
try {
const privateKey = fs.readFileSync("privatekey.pem");
const certificate = fs.readFileSync("certificate.pem");
if (app) {
return https.createServer(
{
key: privateKey,
cert: certificate,
},
app
);
} else {
return https.createServer({
key: privateKey,
cert: certificate,
});
}
} catch (e) {
if (app) {
return http.createServer(app);
} else {
return http.createServer();
}
}
}
const app = express();
const httpServer = http.createServer(app);
const httpsServer = getHttpsServer(app);
const socketioHttpsServer = getHttpsServer();
app.use(cors(corsOptions));
export const io = addSocketToServer(socketioHttpsServer);
app.get("/external-api/github-ui-release", (req, res) => {
const auth = req.headers.authorization || "";
if (process.env.GITHUB_RELEASE_KEY === auth) {
console.log("π New GitHub UI!");
setTimeout(() => {
console.log("πππ SENDING THE LAUNCH SIGNAL CAPTAIN");
io.sendGlobalMessage({
color: "success",
message:
"A new version of the UI is available! Refresh your browser to use it! π",
});
}, 60000);
}
res.sendStatus(200);
});
socketioHttpsServer.listen(socketIoPort, () => {
console.log(`π Socket ready on ${socketIoPort}`);
});
async function startApolloServer() {
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({
schema,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
ApolloServerPluginDrainHttpServer({ httpServer: httpsServer }),
hiveApollo({
enabled: !!process.env.HIVE_TOKEN,
token: process.env.HIVE_TOKEN ?? "",
reporting: {
author: "chrisinajar",
commit: "automated",
},
usage: {
clientInfo(context) {
if (context.client) {
const { name, version } = context.client;
if (name && version) {
return { name, version };
}
}
return null;
},
},
}),
],
context: async ({ res, req }): Promise<BaseContext> => {
let token = req.headers.authorization;
let name = req.headers["apollographql-client-name"];
if (Array.isArray(name)) {
name = name.join(",");
}
let version = req.headers["apollographql-client-version"];
if (Array.isArray(version)) {
version = version.join(",");
}
const context: BaseContext = {
db,
io,
client: name
? {
name,
version: version ?? "",
}
: null,
};
if (token) {
if (token.toLowerCase().startsWith("bearer ")) {
token = token.substr(7);
}
const data = confirm(token);
if (data) {
context.auth = data;
}
}
return context;
},
});
await server.start();
server.applyMiddleware({
app,
cors: corsOptions,
});
httpServer.listen(port, () => {
console.log(`π Apollo Server ready on ${port}`);
});
httpsServer.listen(httpsPort, () => {
console.log(`π Apollo Server SSL ready on ${httpsPort}`);
});
}
startApolloServer();