-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.ts
46 lines (42 loc) · 1.58 KB
/
server.ts
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 { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
import { WebSocketServer } from 'ws';
import { WebsocketService } from '@/app/api/_service/websocket/websocket-service';
const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT || '3000', 10);
const app = next({ dev, turbo: true, turbopack: true });
const handle = app.getRequestHandler();
const dev_server_upgradeHandler = app.getUpgradeHandler();
app.prepare().then(() => {
if (process.env.NODE_ENV === 'production') {
const env_arr = ['REDIS_HOST', 'REDIS_PORT', 'WEB_URL', 'DATABASE_URL', 'JWT_SECRET', 'NOTI_TOKEN', 'NOTI_HOST'];
env_arr.forEach((key) => {
if (!process.env[key]) {
throw new Error(`ENV ${key} are not set`);
}
});
}
const server = createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl);
});
const wss = new WebSocketServer({ noServer: true });
const onConnect = WebsocketService.getInstance().onConnect;
wss.on('connection', onConnect);
wss.on('error', (err) => {
console.error('Wss Error!', err);
});
server.on('upgrade', (req, socket, head) => {
if (req.url !== '/_next/webpack-hmr') {
wss.handleUpgrade(req, socket, head, (ws_client, request) => {
wss.emit('connection', ws_client, request);
});
} else {
dev_server_upgradeHandler(req, socket, head);
}
});
server.listen(3000, () => {
console.log(`> Server listening at http://localhost:${port} as ${dev ? 'development' : process.env.NODE_ENV}`);
});
});