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 | |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 20 | import * as dotenv from 'dotenv'; |
| 21 | dotenv.config(); |
| 22 | |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 23 | import { createServer } from 'node:http'; |
| 24 | |
| 25 | import log from 'loglevel'; |
| 26 | import { Container } from 'typedi'; |
| 27 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 28 | import { App } from './app.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 29 | import { Creds } from './creds.js'; |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 30 | import { Jamid } from './jamid/jamid.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 31 | import { Vault } from './vault.js'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 32 | import { Ws } from './ws.js'; |
| 33 | |
| 34 | log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace'); |
| 35 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 36 | const port: string | number = 5000; |
| 37 | |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 38 | await Container.get(Creds).build(); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 39 | await Container.get(Vault).build(); |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 40 | const jamid = Container.get(Jamid); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 41 | const app = await Container.get(App).build(); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 42 | const wss = await Container.get(Ws).build(); |
| 43 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 44 | const server = createServer(); |
| 45 | |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 46 | server.on('request', app); |
| 47 | server.on('upgrade', wss); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 48 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 49 | server.listen(port); |
| 50 | server.on('error', onError); |
| 51 | server.on('listening', onListening); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 52 | |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 53 | process.once('SIGTERM', shutdown); |
| 54 | process.once('SIGINT', shutdown); |
| 55 | process.once('SIGUSR2', shutdown); |
| 56 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 57 | function onError(error: NodeJS.ErrnoException) { |
| 58 | if (error.syscall !== 'listen') { |
| 59 | throw error; |
| 60 | } |
| 61 | |
| 62 | const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`; |
| 63 | |
| 64 | switch (error.code) { |
| 65 | case 'EACCESS': |
| 66 | log.error(bind + ' requires elevated privileges'); |
| 67 | process.exit(1); |
| 68 | break; |
| 69 | case 'EADDRINUSE': |
| 70 | log.error(bind + ' is already in use'); |
| 71 | process.exit(1); |
| 72 | break; |
| 73 | default: |
| 74 | throw error; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function onListening() { |
| 79 | const address = server.address(); |
| 80 | const bind = typeof address === 'string' ? `pipe ${address}` : `port ${address?.port}`; |
| 81 | log.debug('Listening on ' + bind); |
| 82 | } |
Misha Krieger-Raynauld | 62a0da9 | 2022-10-22 13:46:59 -0400 | [diff] [blame] | 83 | |
| 84 | function shutdown(signal: NodeJS.Signals) { |
| 85 | log.debug(`${signal} received: shutting down server`); |
| 86 | jamid.stop(); |
| 87 | server.close(() => { |
| 88 | log.debug('Server shut down'); |
| 89 | // Needed in order to actually exit the process as its shutdown is blocked by something else |
| 90 | process.exit(); |
| 91 | }); |
| 92 | } |