-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
index.ts
50 lines (44 loc) · 1.12 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
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { createYoga } from "graphql-yoga";
import uWS from "uWebSockets.js";
import { db, env } from "./core";
import { getIdToken } from "./core/auth";
import { schema } from "./schema";
/**
* GraphQL API server middleware.
* @see https://the-guild.dev/graphql/yoga-server/docs
*/
const yoga = createYoga({
schema,
async context({ request }) {
const token = await getIdToken(request);
return { token };
},
});
/**
* High performance HTTP and WebSocket server based on uWebSockets.js.
* @see https://github.com/uNetworking/uWebSockets
*/
const app = uWS
.App()
// GraphQL API endpoint.
.any("/*", yoga);
/**
* Starts the HTTP server.
*/
export function listen() {
app.listen(env.PORT, () => {
console.log(`Server listening on http://localhost:${env.PORT}/`);
});
return async () => {
app.close();
await db.destroy();
};
}
// Start the server if running in a Cloud Run environment.
if (env.K_SERVICE) {
const close = listen();
process.on("SIGINT", () => close());
process.on("SIGTERM", () => close());
}