This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
137 lines (119 loc) · 3.56 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
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
136
137
const compression = require('compression');
const config = require('config');
const express = require('express');
const path = require('path');
const Problem = require('api-problem');
const querystring = require('querystring');
const keycloak = require('./src/components/keycloak');
const log = require('./src/components/log')(module.filename);
const httpLogger = require('./src/components/log').httpLogger;
const v1Router = require('./src/routes/v1');
const apiRouter = express.Router();
const state = {
ready: true, // No dependencies so application is always ready
shutdown: false
};
const app = express();
app.use(compression());
app.use(express.json({ limit: config.get('server.bodyLimit') }));
app.use(express.urlencoded({ extended: true }));
// Skip if running tests
if (process.env.NODE_ENV !== 'test') {
app.use(httpLogger);
}
// Use Keycloak OIDC Middleware
app.use(keycloak.middleware());
// Block requests until service is ready
app.use((_req, res, next) => {
if (state.shutdown) {
new Problem(503, { details: 'Server is shutting down' }).send(res);
} else if (!state.ready) {
new Problem(503, { details: 'Server is not ready' }).send(res);
} else {
next();
}
});
// Frontend configuration endpoint
apiRouter.use('/config', (_req, res, next) => {
try {
const frontend = config.get('frontend');
res.status(200).json(frontend);
} catch (err) {
next(err);
}
});
// Base API Directory
apiRouter.get('/api', (_req, res) => {
if (state.shutdown) {
throw new Error('Server shutting down');
} else {
res.status(200).json('ok');
}
});
// Host API endpoints
apiRouter.use(config.get('server.apiPath'), v1Router);
app.use(config.get('server.basePath'), apiRouter);
// Host the static frontend assets
const staticFilesPath = config.get('frontend.basePath');
app.use('/favicon.ico', (_req, res) => { res.redirect(`${staticFilesPath}/favicon.ico`); });
app.use(staticFilesPath, express.static(path.join(__dirname, 'frontend/dist')));
// Handle 500
// eslint-disable-next-line no-unused-vars
app.use((err, _req, res, _next) => {
if (err.stack) {
log.error(err);
}
if (err instanceof Problem) {
err.send(res, null);
} else {
new Problem(500, 'Server Error', {
detail: (err.message) ? err.message : err
}).send(res);
}
});
// Handle 404
app.use((req, res) => {
if (req.originalUrl.startsWith(`${config.get('server.basePath')}/api`)) {
// Return a 404 problem if attempting to access API
new Problem(404, 'Page Not Found', {
detail: req.originalUrl
}).send(res);
} else {
// Redirect any non-API requests to static frontend with redirect breadcrumb
const query = querystring.stringify({ ...req.query, r: req.path });
res.redirect(`${staticFilesPath}/?${query}`);
}
});
// Prevent unhandled errors from crashing application
process.on('unhandledRejection', err => {
if (err && err.stack) {
log.error(err);
}
});
// Graceful shutdown support
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
process.on('SIGUSR1', shutdown);
process.on('SIGUSR2', shutdown);
process.on('exit', () => {
log.info('Exiting...');
});
/**
* @function shutdown
* Shuts down this application after at least 3 seconds.
*/
function shutdown() {
log.info('Received kill signal. Shutting down...');
// Wait 3 seconds before starting cleanup
if (!state.shutdown) setTimeout(cleanup, 3000);
}
/**
* @function cleanup
* Cleans up connections in this application.
*/
function cleanup() {
log.info('Service no longer accepting traffic');
state.shutdown = true;
process.exit();
}
module.exports = app;