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 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 25 | import { App } from './app.js'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 26 | import { Ws } from './ws.js'; |
| 27 | |
| 28 | log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace'); |
| 29 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 30 | const port: string | number = 5000; |
| 31 | |
| 32 | const app = await Container.get(App).build(); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 33 | const wss = await Container.get(Ws).build(); |
| 34 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 35 | const server = createServer(); |
| 36 | |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 37 | server.on('request', app); |
| 38 | server.on('upgrade', wss); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 39 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 40 | server.listen(port); |
| 41 | server.on('error', onError); |
| 42 | server.on('listening', onListening); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 43 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 44 | function onError(error: NodeJS.ErrnoException) { |
| 45 | if (error.syscall !== 'listen') { |
| 46 | throw error; |
| 47 | } |
| 48 | |
| 49 | const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`; |
| 50 | |
| 51 | switch (error.code) { |
| 52 | case 'EACCESS': |
| 53 | log.error(bind + ' requires elevated privileges'); |
| 54 | process.exit(1); |
| 55 | break; |
| 56 | case 'EADDRINUSE': |
| 57 | log.error(bind + ' is already in use'); |
| 58 | process.exit(1); |
| 59 | break; |
| 60 | default: |
| 61 | throw error; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function onListening() { |
| 66 | const address = server.address(); |
| 67 | const bind = typeof address === 'string' ? `pipe ${address}` : `port ${address?.port}`; |
| 68 | log.debug('Listening on ' + bind); |
| 69 | } |