-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (70 loc) · 2.3 KB
/
Copy pathserver.js
File metadata and controls
81 lines (70 loc) · 2.3 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
import { config } from 'dotenv';
config(); // Load .env before anything else
import { Server } from 'node:http';
import { parse } from 'node:url';
import next from 'next';
import { setHttpServer, setWebSocketServer } from 'next-ws/server';
import { WebSocketServer } from 'ws';
import * as esbuild from 'esbuild';
const requiredEnv = ['DATABASE_URL'];
for (const key of requiredEnv) {
if (!process.env[key]) {
console.error(`Missing required env var: ${key}`);
process.exit(1);
}
}
import './game/headless.js';
const dev = process.env.NODE_ENV !== 'production';
if (dev) {
esbuild.context({
entryPoints: ['./public/client/main.js'],
bundle: true,
outfile: './public/game.bundle.js',
minify: true,
sourcemap: true
}).then((context) => {
console.log('Bundling game...');
return context.watch().then(() => {
console.log('Game bundle watching.');
});
}).catch(err => console.error('esbuild error:', err));
} else {
esbuild.build({
entryPoints: ['./public/client/main.js'],
bundle: true,
outfile: './public/game.bundle.js',
minify: true,
sourcemap: true
}).then(() => console.log('Game bundle built.')).catch(err => console.error('esbuild error:', err));
}
//import './game/index.js'
const httpServer = new Server();
setHttpServer(httpServer);
const webSocketServer = new WebSocketServer({ noServer: true });
setWebSocketServer(webSocketServer);
const hostname = 'localhost';
const port = Number.parseInt(process.env.PORT ?? '9000');
const app = next({ dev, hostname, port, customServer: true });
const handle = app.getRequestHandler();
app.prepare().then(() => {
httpServer
.on('request', async (req, res) => {
const parsedUrl = parse(req.url, true);
res.setHeader('fly-region', process.env.FLY_REGION || 'offsite');
await handle(req, res, parsedUrl);
}).on('upgrade', (req, socket, head) => {
if (req.url === '/_next/webpack-hmr') {
socket.destroy();
}
})
.listen(port, () => {
console.log(` ▲ Ready on http://${hostname}:${port}`);
// Trigger Discord bot init
fetch(`http://localhost:${port}/api/discord-init`).catch(() => {});
/*setInterval(() => {
fetch('http://localhost:9000/api/lobby/empty', {
method: 'POST'
});
}, 10000);*/
});
});