-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.js
49 lines (42 loc) · 1.32 KB
/
app.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
"use strict";
/* eslint-disable no-console */
const Koa = require("koa");
const KoaRouter = require("@koa/router");
const koaHandlebars = require("koa-handlebars");
const koaBodyParser = require("koa-bodyparser");
const handlebars = require("handlebars");
const httpGracefulShutdown = require("http-graceful-shutdown");
const routes = require("./routes.js");
const csp = require("./server-infra/csp.js");
const errorHandler = require("./server-infra/error-handler.js");
const handlebarsSectionHelper = require("./server-infra/handlebars-section-helper.js");
const headers = require("./server-infra/headers.js");
const app = new Koa();
const router = new KoaRouter();
for (const route of routes) {
const routerMethod = route.method.toLowerCase();
router[routerMethod](route.path, route.handler);
}
app
.use(koaBodyParser())
.use(koaHandlebars({
defaultLayout: "whatwg",
handlebars,
helpers: {
section: handlebarsSectionHelper
}
}))
.use(csp)
.use(errorHandler)
.use(headers)
.use(router.routes())
.use(router.allowedMethods());
const port = process.env.PORT || 3000;
const server = app.listen(port);
console.log(`Running site at: http://localhost:${port}`);
httpGracefulShutdown(server, {
finally() {
console.log(`Gracefully shut down the server on ${port}`);
}
});
module.exports = server;