/
server.js
72 lines (63 loc) · 1.87 KB
/
server.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
const express = require('express');
const proxy = require('http-proxy-middleware');
const path = require("path");
const app = express();
const port = 4321;
console.log("Environment:", process.env.ENV);
let apiHost;
let sseHost;
switch (process.env.ENV) {
case 'docker':
apiHost = 'http://host.docker.internal:7575';
sseHost = 'http://host.docker.internal:5002';
break;
case 'dev':
case 'production':
apiHost = 'http://linker-api:7575';
sseHost = 'http://beee-eventgo:5002';
break;
default:
apiHost = 'http://localhost:7575';
sseHost = 'http://localhost:5002';
}
app.use('/', express.static(path.join(__dirname, 'dist')));
app.use('/event/publish', proxy({
target: `${sseHost}`,
pathRewrite: {'^/event': '/api'},
changeOrigin: true,
}));
app.use('/api', proxy({
target: `${apiHost}`,
pathRewrite: {'^/api': '/api/v0'},
changeOrigin: true,
}));
// for testing
// app.get('/sleep', (req, res) => {
// let duration = 30 * 1000;
// if (req.query.duration) {
// duration = req.query.duration * 1000;
// }
// console.log(`Sleeping for ${duration} milliseconds`);
// setTimeout(() => res.send(`Slept for ${duration} milliseconds`), duration);
// });
app.get('/health_check', (req, res) => {
// console.log('Health ping');
res.send('Health pong');
});
const server = app.listen(port, () => console.log(`Listening on port ${port}!`));
const handleShutdown = (signal) => {
console.log(`[SERVER] shutdown initiated... (${signal} received)`);
server.getConnections((err, count) => {
console.log(`[SERVER] number of concurrent connections: ${count}`);
});
server.close(function(err) {
if (err) {
console.log('[SERVER] error in shutdown:', err);
return process.exit(1);
}
console.log('[SERVER] server exiting');
process.exit(0);
});
}
process.on('SIGINT', handleShutdown);
process.on('SIGTERM', handleShutdown);