forked from nditech/CyberSim-Backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (51 loc) · 1.49 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
const { createServer } = require('http');
const db = require('./src/models/db');
const app = require('./src/app');
const createSocket = require('./src/socketio');
const logger = require('./src/logger');
const checkEnviroment = async () => {
if (!process.env.NODE_ENV) {
throw new Error('NODE_ENV must be set!');
}
if (!process.env.DB_URL) {
throw new Error('DB_URL must be set!');
}
if (process.env.NODE_ENV === 'test') {
await db.migrate.rollback({}, true);
await db.migrate.latest();
await db.seed.run();
logger.info('Database successfully reseted');
} else {
await db.migrate.latest();
}
};
checkEnviroment()
.then(() => {
const port = process.env.PORT || 8080;
const http = createServer(app);
createSocket(http);
const server = http.listen(port, () => {
logger.info(`Server is running at port: ${port}`);
});
let shuttingDown = false;
const gracefulShutdown = async () => {
logger.info('Got kill signal, starting graceful shutdown');
if (shuttingDown) {
return;
}
shuttingDown = true;
server.close((err) => {
if (err) {
logger.error('Error happend during graceful shutdown: %s', err);
process.exit(1);
}
logger.info('Graceful shutdown finished.');
process.exit(0);
});
};
process.on('SIGTERM', gracefulShutdown);
})
.catch((err) => {
logger.error('Enviroment check is unsuccessful: %s', err);
process.exit(1);
});