Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | import 'reflect-metadata'; |
| 19 | |
| 20 | import { createServer } from 'node:http'; |
| 21 | |
| 22 | import log from 'loglevel'; |
| 23 | import { Container } from 'typedi'; |
| 24 | |
| 25 | import { Router } from './router.js'; |
| 26 | import { Ws } from './ws.js'; |
| 27 | |
| 28 | log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace'); |
| 29 | |
| 30 | const app = await Container.get(Router).build(); |
| 31 | const wss = await Container.get(Ws).build(); |
| 32 | |
| 33 | // Disable HTTP 1.1 Keep-Alive |
| 34 | const server = createServer((_, res) => res.setHeader('Connection', 'close')); |
| 35 | server.on('request', app); |
| 36 | server.on('upgrade', wss); |
| 37 | server.listen({ |
| 38 | host: '0.0.0.0', |
| 39 | port: 5000, |
| 40 | exclusive: true, |
| 41 | }); |
| 42 | |
| 43 | log.debug('Server started (HTTP + WS)'); |
| 44 | |
| 45 | const closeFn: NodeJS.SignalsListener = (signal) => { |
| 46 | log.info(signal); |
| 47 | server.close(); |
| 48 | log.info('server closed'); |
| 49 | }; |
| 50 | process.once('SIGTERM', closeFn); |
| 51 | process.once('SIGHUP', closeFn); |
| 52 | process.once('SIGINT', closeFn); |