-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
46 lines (38 loc) · 1.27 KB
/
Copy pathserver.ts
File metadata and controls
46 lines (38 loc) · 1.27 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
import http from 'http';
import next from 'next';
import { parse } from 'url';
import { WebSocketManager } from './src/lib/ws/manager';
import { setWebSocketManager } from './src/lib/ws/broadcast';
import { seedDefaultsIfNeeded } from './src/lib/db/seed-defaults';
const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT ?? '3000', 10);
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const handleUpgrade = app.getUpgradeHandler();
seedDefaultsIfNeeded();
const wsManager = new WebSocketManager();
setWebSocketManager(wsManager);
wsManager.start();
const server = http.createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl);
});
server.on('upgrade', (req, socket, head) => {
const { pathname } = parse(req.url!);
if (pathname === '/ws') {
wsManager.handleUpgrade(req, socket as import('net').Socket, head);
} else {
handleUpgrade(req, socket, head);
}
});
server.listen(port, () => {
console.log('> Ready on http://localhost:' + port);
});
const shutdown = () => {
wsManager.stop();
server.close(() => process.exit(0));
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
});