-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.ts
57 lines (44 loc) · 1.43 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
/* SPDX-FileCopyrightText: 2016-present Kriasoft <[email protected]> */
/* SPDX-License-Identifier: MIT */
import chalk from "chalk";
import express from "express";
import { NotFound } from "http-errors";
import { auth } from "../auth";
import { handleError } from "./errors";
import { graphql, updateSchema } from "./graphql";
import { withViews } from "./views";
export 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", graphql);
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 = chalk.greenBright(process.env.APP_ENV);
const dbName = chalk.greenBright(process.env.PGDATABASE);
const url = chalk.blueBright(`http://localhost:${port}/`);
const server = api.listen(port, function () {
console.log(`Listening on ${url} (env: ${envName}, db: ${dbName})`);
});
process.once("SIGTERM", function () {
server.close();
});
}