This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (64 loc) · 2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require('dotenv').config();
const bunyan = require('bunyan');
const express = require('express');
const bodyParser = require('body-parser');
require('body-parser-csv')(bodyParser);
const handleHealth = require('./src/handlers/handleHealth');
const handleReadiness = require('./src/handlers/handleReadiness');
const handleBalance = require('./src/handlers/handleBalance');
const handleCredits = require('./src/handlers/handleCredits');
const logger = bunyan.createLogger({
level: 'info',
name: `tour-operator-credits-service-log`,
});
const createEndpoints = require('./src/createEndpoints');
const port = 3000;
const app = express();
app.use(
bodyParser.csv({
csvParseOptions: {
fastcsvParams: {},
},
})
);
app.enable('trust proxy');
const serverState = {
readyToReceiveTraffic: true,
};
const server = app.listen(port, () => {
logger.info(`app is listening on port ${port}`);
});
createEndpoints({
app,
serverState,
logger,
handleHealth,
handleReadiness,
handleBalance,
handleCredits,
});
app.use((err, req, res, next) => {
logger.error(err, 'Unexpected error');
res.status(500).send('Internal Server Error');
});
if (process.env.NODE_ENV === 'production') {
process.on('SIGTERM', () => {
const timeForLoadBalancerToReact = 4000;
const readinessCheckPeriod = 2000;
const timeToWaitBeforeShutdown =
4 * readinessCheckPeriod + timeForLoadBalancerToReact;
logger.info(
`Waiting ${timeToWaitBeforeShutdown}ms before initialising graceful shutdown...`
);
serverState.readyToReceiveTraffic = false;
setTimeout(async () => {
logger.info(
'Waiting to close outstanding connections to shut down gracefully...'
);
server.close(() => {
logger.info('Graceful shutdown completed successfully.');
});
}, timeToWaitBeforeShutdown);
});
process.on('SIGINT', process.exit);
}