-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.ts
54 lines (45 loc) · 1.38 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
47
48
49
50
51
52
53
54
/* eslint-disable @typescript-eslint/no-var-requires */
import fastify, { FastifyInstance } from 'fastify';
import { ServerOptions } from 'ws';
import { WebsocketPluginOptions } from 'fastify-websocket';
import {
WS_MAX_PAYLOAD,
HTTP_PORT,
NODE_ENV,
FASTIFY_LOG_ENABLED
} from '@app/config';
import { getWsAddressFromServer } from '@app/utils';
const { version } = require('../package.json');
const app = fastify({ logger: NODE_ENV === 'dev' || FASTIFY_LOG_ENABLED });
// Provides a health endpoint to check
app.register(require('./plugins/health'), {
options: {
version
}
});
// Allows testing of cloud events
app.register(require('./plugins/events'));
// Register the WS plugin, apply a max payload limit, and optional authorisation
app.register(require('fastify-websocket'), {
options: {
maxPayload: WS_MAX_PAYLOAD,
verifyClient: (info, next) => {
// Can add optional verification logic into this block
next(true);
}
} as ServerOptions
} as WebsocketPluginOptions);
// Expose the game WS endpoint
app.register(require('./plugins/game'));
export default async function startServer(): Promise<FastifyInstance> {
try {
await app.listen(HTTP_PORT, '0.0.0.0');
app.log.info(
`connect via WebSocket to ws://${getWsAddressFromServer(app.server)}/game`
);
return app;
} catch (err) {
app.log.error(err);
process.exit(1);
}
}