blob: 579e9ef561177ae71c95681dff3cb6145eae984e [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-Raynauld242560f2022-10-16 19:59:58 -040027import { Vault } from './vault.js';
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000028import { Ws } from './ws.js';
29
30log.setLevel(process.env.NODE_ENV === 'production' ? 'error' : 'trace');
31
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040032const port: string | number = 5000;
33
Issam E. Maghnif796a092022-10-09 20:25:26 +000034await Container.get(Creds).build();
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040035await Container.get(Vault).build();
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040036const app = await Container.get(App).build();
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000037const wss = await Container.get(Ws).build();
38
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040039const server = createServer();
40
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000041server.on('request', app);
42server.on('upgrade', wss);
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000043
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040044server.listen(port);
45server.on('error', onError);
46server.on('listening', onListening);
Issam E. Maghni0ef4a362022-10-05 23:20:16 +000047
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040048function onError(error: NodeJS.ErrnoException) {
49 if (error.syscall !== 'listen') {
50 throw error;
51 }
52
53 const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
54
55 switch (error.code) {
56 case 'EACCESS':
57 log.error(bind + ' requires elevated privileges');
58 process.exit(1);
59 break;
60 case 'EADDRINUSE':
61 log.error(bind + ' is already in use');
62 process.exit(1);
63 break;
64 default:
65 throw error;
66 }
67}
68
69function onListening() {
70 const address = server.address();
71 const bind = typeof address === 'string' ? `pipe ${address}` : `port ${address?.port}`;
72 log.debug('Listening on ' + bind);
73}