This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-factory.js
135 lines (119 loc) · 4.29 KB
/
app-factory.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const restify = require('restify');
const SwaggerRestifyMw = require('swagger-restify-mw');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const elasticApmNode = require('elastic-apm-node');
const { ResponseJsonMiddleware } = require('./middlewares/response-json-middleware');
const verifyIntegrityErrors = ({ integrityCheckers }) => {
const errors = integrityCheckers.map((checker) => {
try {
checker();
return null;
} catch (error) {
return error;
}
}).filter(error => error);
if (errors.length) {
throw new Error(errors
.reduce((acc, error) => `${acc || ''}${error.message}\n`, ''));
}
};
const injectSwaggerToApp = ({ app, appRoot, swaggerFile }) => new Promise((resolve, reject) => {
SwaggerRestifyMw.create({ appRoot, swaggerFile }, (err, swaggerRestify) => {
if (err) {
reject(err);
return;
}
swaggerRestify.register(app);
resolve();
});
});
// Kubernetes will wait for up to the grace period before forcibly killing the process
const GRACE_PERIOD = 29 * 1000;
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const registerGracefulShutdown = ({ app, closeSequelize, logger }) => {
let initTime;
const gracefulShutdown = async (signal) => {
initTime = new Date();
logger.info(`Starting app graceful shutdown after signal ${signal}`);
let exitCode = 0
if (process.env.NODE_ENV === 'production') {
await sleep(GRACE_PERIOD);
}
try {
await Promise.all([
new Promise((resolve) => app.close(resolve)),
closeSequelize(),
]);
} catch (err) {
exitCode = 1;
logger.error(err);
}
const stopTime = new Date();
logger.info(`Shutdown graceful ${stopTime - initTime} ms after signal`);
process.exit(exitCode);
};
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
process.on('exit', (code) => {
const stopTime = new Date();
logger.info(`Process exit ${stopTime - initTime} ms after signal`);
logger.info(`About to exit with code: ${code}`);
});
};
const notifyProcessInitializedGracefully = () => {
// for more information see https://pm2.io/doc/en/runtime/best-practices/graceful-shutdown/
if (process.send) process.send('ready');
};
const setupElasticApm = ({ logger, elasticApmEnabled }) => {
logger.info(`ElasticSearch APM enabled: ${elasticApmEnabled || false}`);
if (elasticApmEnabled) {
logger.info(`Init ElasticSearch APM enabled: ${process.env.ES_APM_ENABLED}`);
elasticApmNode.start({
serviceName: process.env.APM_SERVICE_NAME || 'default',
serverUrl: process.env.APM_SERVICE_HOST,
});
}
};
module.exports.createApp = () => {
return restify.createServer();
};
module.exports.injectMiddlewaresAndListen = async ({
app, isSentryEnabled, isDevelopment, isLogRequestEnabled, logger, sentry,
closeSequelize, port, env, appRoot, swaggerFile, isElasticApmEnabled, isNewRelicApmEnabled,
requestTraceMiddleware, prometheusMiddleware, auditTrailMiddleware, integrityCheckers = [],
}) => {
verifyIntegrityErrors({ integrityCheckers });
if (isSentryEnabled) {
app.use(sentry.Handlers.requestHandler());
}
if (isLogRequestEnabled) {
if (isDevelopment) {
app.use(morgan('dev'));
} else {
app.use(morgan('combined'));
}
}
logger.info(`NewRelic APM enabled: ${isNewRelicApmEnabled}`);
setupElasticApm({ logger, elasticApmEnabled: isElasticApmEnabled });
app.use(requestTraceMiddleware);
app.use(prometheusMiddleware.requestCounters);
app.use(prometheusMiddleware.responseCounters);
app.use(ResponseJsonMiddleware);
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(restify.plugins.multipartBodyParser());
app.use((req, res, next) => {
res.header('Strict-Transport-Security', 'max-age=63072000; includeSubDomains; preload')
return next();
})
prometheusMiddleware.injectMetricsRoute(app);
await injectSwaggerToApp({ app, appRoot, swaggerFile });
app.on('after', auditTrailMiddleware);
app.listen(port);
logger.info(`API running on http://localhost:${port}`);
logger.info(`API running on ${env.toUpperCase()} mode.`);
notifyProcessInitializedGracefully();
registerGracefulShutdown({ app, closeSequelize, logger });
return app;
};