-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (38 loc) · 1.21 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
const HyperExpress = require('hyper-express')
const process = require('process')
const chains = require('./chains')
const webserver = new HyperExpress.Server()
const port = +(process.env.PORT ?? 5001)
async function main() {
console.time('Api Server init')
webserver.use((_req, res, next) => {
res.append('Access-Control-Allow-Origin', '*');
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
});
const router = new HyperExpress.Router()
webserver.use(router)
chains.setRoutes(router)
webserver.listen(port)
.then(() => {
console.timeEnd('Api Server init')
console.log('Webserver started on port ' + port)
// process.send('ready')
})
.catch((e) => console.log('Failed to start webserver on port ' + port, e))
}
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
process.on('uncaughtException', (e) => {
console.error('Uncaught exception', e)
shutdown()
})
function shutdown() {
console.log('Shutting down gracefully...');
setTimeout(() => process.exit(0), 5000); // wait 5 seconds before forcing shutdown
webserver.close(() => {
console.log('Server has been shut down gracefully');
process.exit(0);
})
}
main()