-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
99 lines (81 loc) · 2.78 KB
/
server.mjs
File metadata and controls
99 lines (81 loc) · 2.78 KB
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
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import restify from 'restify';
import cookieParser from 'restify-cookies';
import dotenv from 'dotenv';
import session from 'express-session';
import connectMongo from 'connect-mongo';
import Terminus from "@godaddy/terminus";
import get from './routes/get.mjs';
import getToken from './routes/token/get.mjs';
const { createTerminus } = Terminus;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const MongoStore = connectMongo(session);
const ENV_PATH_CURRENT = resolve(__dirname, '../.env');
const ENV_PATH_PARENT = resolve(__dirname, '../../.env');
const isCurrent = fs.existsSync(ENV_PATH_CURRENT);
const isParent = fs.existsSync(ENV_PATH_PARENT);
if (isCurrent) dotenv.config({ path: ENV_PATH_CURRENT });
if (isParent) dotenv.config({ path: ENV_PATH_PARENT });
dotenv.config();
const {
MONGODB_PORT,
MONGODB_HOST,
MONGODB_NAME,
COOKIE_SECRET,
NODE_ENV,
} = process.env;
const PORT = process.env.PORT || 3000;
const server = restify.createServer();
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.gzipResponse());
server.use(cookieParser.parse);
server.use(session({
store: new MongoStore({
url: `mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_NAME}`,
}),
saveUninitialized: true,
resave: false,
cookie: {
secure: false,
},
secret: COOKIE_SECRET,
}));
server.pre((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
res.charSet('utf-8');
return next();
});
server.get('/', get);
server.get('/token/', getToken);
const serverStarted = server.listen(PORT, (error) => {
if (error) throw error;
console.log(`🚀🚀 Ready on http://localhost:${PORT}`);
});
const checkConnections = (resolveConnections, rejectConnections) => {
// eslint-disable-next-line consistent-return
serverStarted.getConnections((error, count) => {
if (error) return rejectConnections();
if (count === 0 || NODE_ENV === "local") return resolveConnections();
console.log(`⌛ Waiting for ${count} open connections to finish`);
setTimeout(checkConnections, 5000, resolveConnections, rejectConnections);
});
};
createTerminus(serverStarted, {
signals: ["SIGTERM", "SIGINT"],
healthChecks: {
"/health": async () => Promise.resolve({ uptime: process.uptime() }),
verbatim: true,
},
beforeShutdown() {
console.log(`💀 Received kill signal`);
return new Promise(checkConnections);
},
});