-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (123 loc) · 3.13 KB
/
server.js
File metadata and controls
148 lines (123 loc) · 3.13 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
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
138
139
140
141
142
143
144
145
146
147
148
'use strict';
require('dotenv').config();
const http = require('http');
const createApp = require('./app');
const createSocketServer = require('./socket');
const { createRedisClient } = require('./redis');
const createCleanupRooms = require('./lib/cleanupRooms');
const DEFAULT_PORT = 3000;
const CLEANUP_DAYS = 30;
const CLEANUP_INTERVAL_MS = 24 * 60 * 60 * 1000;
function readRequiredEnv(name) {
const value = process.env[name];
if (typeof value !== 'string' || value.trim() === '') {
return null;
}
return value.trim();
}
function parsePort(value, fallback) {
const n = Number.parseInt(String(value ?? ''), 10);
if (Number.isFinite(n) && n > 0 && n <= 65535) {
return n;
}
return fallback;
}
function closeHttpServer(server) {
return new Promise((resolve) => {
try {
server.close(() => resolve());
} catch {
resolve();
}
});
}
async function closeRedisClient(client) {
if (!client) {
return;
}
try {
if (typeof client.quit === 'function') {
await client.quit();
return;
}
} catch (err) {
console.error('Redis quit failed', err);
}
try {
if (typeof client.disconnect === 'function') {
client.disconnect();
}
} catch (err) {
console.error('Redis disconnect failed', err);
}
}
const PORT = parsePort(process.env.PORT, DEFAULT_PORT);
const REDIS_URL = readRequiredEnv('REDIS_URL');
const ADMIN_PASS = readRequiredEnv('ADMIN_PASS');
const FRONTEND_URL = readRequiredEnv('FRONTEND_URL');
const missing = Object.entries({ ADMIN_PASS, REDIS_URL, FRONTEND_URL })
.filter(([, value]) => !value)
.map(([key]) => key);
if (missing.length > 0) {
console.error(`Missing env: ${missing.join(', ')}`);
process.exit(1);
}
let redisClient;
try {
redisClient = createRedisClient(REDIS_URL);
} catch (err) {
console.error('Failed to create Redis client', err);
process.exit(1);
}
const httpServer = http.createServer();
const io = createSocketServer({
httpServer,
redisClient,
frontendUrl: FRONTEND_URL,
});
const app = createApp({
redisClient,
io,
adminPass: ADMIN_PASS,
frontendUrl: FRONTEND_URL,
});
httpServer.on('request', app);
let cleanupInterval = null;
try {
const cleanup = createCleanupRooms({
redisClient,
io,
thresholdDays: CLEANUP_DAYS,
});
cleanupInterval = cleanup.schedule(CLEANUP_INTERVAL_MS);
} catch (err) {
console.error('Failed to initialize cleanup service', err);
}
let shuttingDown = false;
async function shutdown(signal) {
if (shuttingDown) {
return;
}
shuttingDown = true;
console.log(`Received ${signal}, shutting down...`);
if (cleanupInterval) {
clearInterval(cleanupInterval);
cleanupInterval = null;
}
await Promise.allSettled([
closeHttpServer(httpServer),
new Promise((resolve) => io.close(() => resolve())),
io.closeRedisConnections ? io.closeRedisConnections() : Promise.resolve(),
closeRedisClient(redisClient),
]);
process.exit(0);
}
process.once('SIGINT', () => {
void shutdown('SIGINT');
});
process.once('SIGTERM', () => {
void shutdown('SIGTERM');
});
httpServer.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});