-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (37 loc) · 1.09 KB
/
Copy pathserver.js
File metadata and controls
50 lines (37 loc) · 1.09 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
require('dotenv').config();
const prisma = require('./db');
const { loadEnv } = require('./config/env');
const { buildApp } = require('./app');
const { startMqttWorker } = require('./services/mqtt');
async function shutdown(server, client, signal) {
console.log(`shutdown requested: ${signal}`);
if (client) {
client.end(true);
}
await new Promise((resolve) => {
server.close(() => resolve());
});
await prisma.$disconnect();
}
async function main() {
const config = loadEnv();
if (!config.databaseUrl) {
throw new Error('DATABASE_URL is required');
}
await prisma.$connect();
const app = buildApp();
const server = app.listen(config.port, () => {
console.log(`api listening on ${config.port}`);
});
const mqttClient = startMqttWorker(config);
process.on('SIGINT', () => {
shutdown(server, mqttClient, 'SIGINT').finally(() => process.exit(0));
});
process.on('SIGTERM', () => {
shutdown(server, mqttClient, 'SIGTERM').finally(() => process.exit(0));
});
}
main().catch((err) => {
console.error(err.message || err);
process.exit(1);
});