forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (48 loc) · 1.59 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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import SendGrid from "@sendgrid/mail";
import express from "express";
import { NotFound } from "http-errors";
import { auth } from "./auth";
import db from "./db";
import env from "./env";
import { handleError } from "./errors";
import { handleGraphQL, updateSchema } from "./graphql";
import { withViews } from "./views";
SendGrid.setApiKey(env.SENDGRID_API_KEY);
const api = withViews(express());
api.enable("trust proxy");
api.disable("x-powered-by");
// OAuth 2.0 authentication endpoints and user sessions
api.use(auth);
// GraphQL API middleware
api.use("/api", express.json(), handleGraphQL);
api.get("/", (req, res) => {
res.render("home");
});
api.get("/favicon.ico", function (req, res) {
res.redirect("https://nodejs.org/static/images/favicons/favicon.ico");
});
api.get("*", function () {
throw new NotFound();
});
api.use(handleError);
/**
* Launch API for testing when in development mode.
*
* NOTE: This block will be removed from production build by Rollup.
*/
if (process.env.NODE_ENV === "development") {
updateSchema();
const port = process.env.PORT ?? 8080;
const envName = `\x1b[92m${process.env.APP_ENV}\x1b[0m`;
const dbName = `\x1b[92m${process.env.PGDATABASE}\x1b[0m`;
const url = `\x1b[94mhttp://localhost:${port}/\x1b[0m`;
const server = api.listen(port, function () {
console.log(`Listening on ${url} (env: ${envName}, db: ${dbName})`);
});
process.once("SIGTERM", function () {
server.close();
});
}
export { api, db, env, updateSchema };