-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
54 lines (42 loc) · 1.71 KB
/
index.js
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
import 'dotenv/config';
import { ipFilterMiddleware, ipFilterError } from './server/moderation/ip-filter.js';
import { WEBSOCKET_MAX_PAYLOAD, COOKIE_MAX_AGE } from './constants.js';
import indexRouter from './routes/index.js';
import webhookRouter from './routes/api/webhook.js';
import handleWssConnection from './server/multiplayer/handle-wss-connection.js';
import hostnameRedirection from './server/hostname-redirection.js';
import httpsEnforcement from './server/https-enforcement.js';
import cookieSession from 'cookie-session';
import express from 'express';
import morgan from 'morgan';
import { createServer } from 'http';
import { WebSocketServer } from 'ws';
const app = express();
const server = createServer(app);
const port = process.env.PORT || 3000;
const wss = new WebSocketServer({ server, maxPayload: WEBSOCKET_MAX_PAYLOAD });
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'));
}
// https://stackoverflow.com/questions/10348906/how-to-know-if-a-request-is-http-or-https-in-node-js
app.enable('trust proxy');
app.use(hostnameRedirection);
app.use(httpsEnforcement);
// See https://masteringjs.io/tutorials/express/query-parameters
// for why we use 'simple'
app.set('query parser', 'simple');
app.use('/api/webhook', express.raw({ type: '*/*' }), webhookRouter);
app.use(express.json());
app.use(cookieSession({
name: 'session',
keys: [process.env.SECRET_KEY_1 ?? 'secretKey1', process.env.SECRET_KEY_2 ?? 'secretKey2'],
maxAge: COOKIE_MAX_AGE
}));
app.use(ipFilterMiddleware);
app.use(ipFilterError);
wss.on('connection', handleWssConnection);
app.use(indexRouter);
// listen on ipv4 instead of ipv6
server.listen({ port, host: '0.0.0.0' }, () => {
console.log(`listening at port=${port}`);
});