blob: f6086cd0bb9f9f0ca4c469898a959f2c9c5a2734 [file] [log] [blame]
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -04001/*
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 */
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040018import cors from 'cors';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040019import express, { json, NextFunction, Request, Response } from 'express';
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040020import helmet from 'helmet';
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040021import { HttpStatusCode } from 'jami-web-common';
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040022import log from 'loglevel';
23import { Service } from 'typedi';
24
Charlie2bc0d672022-11-04 11:53:44 -040025import { bindWebRTCCallbacks } from './handlers/webrtc-handler.js';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050026import { checkAdminSetup } from './middleware/setup.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040027import { accountRouter } from './routers/account-router.js';
28import { authRouter } from './routers/auth-router.js';
Misha Krieger-Raynauld7f959332022-11-04 15:12:53 -040029import { callRouter } from './routers/call-router.js';
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040030import { contactsRouter } from './routers/contacts-router.js';
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040031import { conversationRouter } from './routers/conversation-router.js';
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040032import { defaultModeratorsRouter } from './routers/default-moderators-router.js';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040033import { nameserverRouter } from './routers/nameserver-router.js';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050034import { setupRouter } from './routers/setup-router.js';
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040035
36@Service()
Issam E. Maghnif796a092022-10-09 20:25:26 +000037export class App {
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040038 async build() {
39 const app = express();
40
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040041 // Setup middleware
42 app.use(helmet());
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040043 app.use(cors());
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040044 app.use(json());
45
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050046 // Enforce admin setup
47 app.use('/setup', setupRouter);
48 app.use(checkAdminSetup);
49
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040050 // Setup routing
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040051 app.use('/auth', authRouter);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040052 app.use('/account', accountRouter);
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040053 app.use('/contacts', contactsRouter);
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040054 app.use('/default-moderators', defaultModeratorsRouter);
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040055 app.use('/conversations', conversationRouter);
Misha Krieger-Raynauld7f959332022-11-04 15:12:53 -040056 app.use('/calls', callRouter);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040057 app.use('/ns', nameserverRouter);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040058
Charlie2bc0d672022-11-04 11:53:44 -040059 // Setup WebSocket callbacks
60 bindWebRTCCallbacks();
61
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040062 // Setup 404 error handling
63 app.use((_req, res) => {
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040064 res.sendStatus(HttpStatusCode.NotFound);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040065 });
66
67 // Setup internal error handling
68 app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
69 log.error(err);
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040070 res.status(HttpStatusCode.InternalServerError).send(err.message);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040071 });
72
73 return app;
74 }
75}