-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (54 loc) · 1.31 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const Hapi = require("@hapi/hapi");
const routes = require("./routes/routes.js");
const plugins = [require("@hapi/inert")];
let server;
async function start() {
try {
const hapiOptions = {
port: process.env.PORT || 8080,
load: { sampleInterval: 1000 },
routes: {
state: { parse: false, failAction: "log" }
}
};
server = Hapi.server(hapiOptions);
await server.register(plugins);
server.route(routes);
if (process.env.FEATURE_BROTLI) {
await server.register({
plugin: require("brok"),
options: {
compress: {
quality: 5
}
}
});
}
await server.start();
} catch (err) {
// console.log(err.message, err.stack);
throw err;
}
}
start()
.then(() => {
console.log("hapi server running " + server.info.uri);
})
.catch(err => {
console.error(err, err.stack);
process.exit(1);
});
async function gracefullyStop() {
console.log("stopping hapi server");
try {
await server.stop({ timeout: 10000 });
console.log("hapi server stopped");
} catch (err) {
console.error(err);
process.exit(1);
}
process.exit(0);
}
// listen on SIGINT and SIGTERM signal and gracefully stop the server
process.on("SIGINT", gracefullyStop);
process.on("SIGTERM", gracefullyStop);