blob: 27e6e990db80105f3117cbee23cfefb36add6a51 [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
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050025import { checkAdminSetup } from './middleware/setup.js';
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040026import { accountRouter } from './routers/account-router.js';
27import { authRouter } from './routers/auth-router.js';
Misha Krieger-Raynauld7f959332022-11-04 15:12:53 -040028import { callRouter } from './routers/call-router.js';
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040029import { contactsRouter } from './routers/contacts-router.js';
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040030import { conversationRouter } from './routers/conversation-router.js';
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040031import { defaultModeratorsRouter } from './routers/default-moderators-router.js';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040032import { nameserverRouter } from './routers/nameserver-router.js';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050033import { setupRouter } from './routers/setup-router.js';
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050034import { bindWebRTCCallbacks } from './websocket/webrtc-handler.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-Raynauldb933fbb2022-11-15 15:11:09 -050038 app = express();
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040039
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050040 constructor() {
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040041 // Setup middleware
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050042 this.app.use(helmet());
43 this.app.use(cors());
44 this.app.use(json());
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040045
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050046 // Enforce admin setup
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050047 this.app.use('/setup', setupRouter);
48 this.app.use(checkAdminSetup);
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050049
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040050 // Setup routing
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050051 this.app.use('/auth', authRouter);
52 this.app.use('/account', accountRouter);
53 this.app.use('/contacts', contactsRouter);
54 this.app.use('/default-moderators', defaultModeratorsRouter);
55 this.app.use('/conversations', conversationRouter);
56 this.app.use('/calls', callRouter);
57 this.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
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050063 this.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
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050068 this.app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040069 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 });
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040072 }
73}