blob: 587438c4f66e594b9b4be00b02ee8deca4afd37a [file] [log] [blame]
Issam E. Maghni0ef4a362022-10-05 23:20:16 +00001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import 'reflect-metadata';
19
20import { createServer } from 'node:http';
21
22import log from 'loglevel';
23import { Container } from 'typedi';
24
25import { Router } from './router.js';
26import { Ws } from './ws.js';
27
28log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace');
29
30const app = await Container.get(Router).build();
31const wss = await Container.get(Ws).build();
32
33// Disable HTTP 1.1 Keep-Alive
34const server = createServer((_, res) => res.setHeader('Connection', 'close'));
35server.on('request', app);
36server.on('upgrade', wss);
37server.listen({
38 host: '0.0.0.0',
39 port: 5000,
40 exclusive: true,
41});
42
43log.debug('Server started (HTTP + WS)');
44
45const closeFn: NodeJS.SignalsListener = (signal) => {
46 log.info(signal);
47 server.close();
48 log.info('server closed');
49};
50process.once('SIGTERM', closeFn);
51process.once('SIGHUP', closeFn);
52process.once('SIGINT', closeFn);