blob: ec7067efb2bd5c2de6c7b1a4d2aa635048ef70d7 [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
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040025import { App } from './app.js';
Issam E. Maghnif796a092022-10-09 20:25:26 +000026import { Creds } from './creds.js';
Misha Krieger-Raynauld62a0da92022-10-22 13:46:59 -040027import { Jamid } from './jamid/jamid.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040028import { Vault } from './vault.js';
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000029import { Ws } from './ws.js';
30
31log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace');
32
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040033const port: string | number = 5000;
34
Issam E. Maghnif796a092022-10-09 20:25:26 +000035await Container.get(Creds).build();
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040036await Container.get(Vault).build();
Misha Krieger-Raynauld62a0da92022-10-22 13:46:59 -040037const jamid = Container.get(Jamid);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040038const app = await Container.get(App).build();
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000039const wss = await Container.get(Ws).build();
40
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040041const server = createServer();
42
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000043server.on('request', app);
44server.on('upgrade', wss);
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000045
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040046server.listen(port);
47server.on('error', onError);
48server.on('listening', onListening);
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000049
Misha Krieger-Raynauld62a0da92022-10-22 13:46:59 -040050process.once('SIGTERM', shutdown);
51process.once('SIGINT', shutdown);
52process.once('SIGUSR2', shutdown);
53
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040054function onError(error: NodeJS.ErrnoException) {
55 if (error.syscall !== 'listen') {
56 throw error;
57 }
58
59 const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
60
61 switch (error.code) {
62 case 'EACCESS':
63 log.error(bind + ' requires elevated privileges');
64 process.exit(1);
65 break;
66 case 'EADDRINUSE':
67 log.error(bind + ' is already in use');
68 process.exit(1);
69 break;
70 default:
71 throw error;
72 }
73}
74
75function onListening() {
76 const address = server.address();
77 const bind = typeof address === 'string' ? `pipe ${address}` : `port ${address?.port}`;
78 log.debug('Listening on ' + bind);
79}
Misha Krieger-Raynauld62a0da92022-10-22 13:46:59 -040080
81function shutdown(signal: NodeJS.Signals) {
82 log.debug(`${signal} received: shutting down server`);
83 jamid.stop();
84 server.close(() => {
85 log.debug('Server shut down');
86 // Needed in order to actually exit the process as its shutdown is blocked by something else
87 process.exit();
88 });
89}