blob: 39dbffa0f7f4e1441838f8de1d7921e59c65e3d2 [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';
idillon18283ac2023-01-07 12:06:42 -050030import { conversationRequestRouter } from './routers/conversation-request-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';
idillona3c2fad2022-12-18 23:49:10 -050033import { linkPreviewRouter } from './routers/link-preview-router.js';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040034import { nameserverRouter } from './routers/nameserver-router.js';
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050035import { setupRouter } from './routers/setup-router.js';
idillon3e378fd2022-12-23 11:48:12 -050036import { bindChatCallbacks } from './websocket/chat-handler.js';
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050037import { bindWebRtcCallbacks } from './websocket/webrtc-handler.js';
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040038
39@Service()
Issam E. Maghnif796a092022-10-09 20:25:26 +000040export class App {
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050041 app = express();
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040042
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050043 constructor() {
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040044 // Setup middleware
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050045 this.app.use(helmet());
46 this.app.use(cors());
47 this.app.use(json());
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040048
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050049 // Enforce admin setup
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050050 this.app.use('/setup', setupRouter);
51 this.app.use(checkAdminSetup);
Michelle Sepkap Sime6967fb92022-11-08 08:39:36 -050052
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040053 // Setup routing
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050054 this.app.use('/auth', authRouter);
55 this.app.use('/account', accountRouter);
56 this.app.use('/contacts', contactsRouter);
57 this.app.use('/default-moderators', defaultModeratorsRouter);
58 this.app.use('/conversations', conversationRouter);
idillon18283ac2023-01-07 12:06:42 -050059 this.app.use('/conversation-requests', conversationRequestRouter);
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050060 this.app.use('/calls', callRouter);
idillona3c2fad2022-12-18 23:49:10 -050061 this.app.use('/link-preview', linkPreviewRouter);
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050062 this.app.use('/ns', nameserverRouter);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040063
Charlie2bc0d672022-11-04 11:53:44 -040064 // Setup WebSocket callbacks
idillon3e378fd2022-12-23 11:48:12 -050065 bindChatCallbacks();
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050066 bindWebRtcCallbacks();
Charlie2bc0d672022-11-04 11:53:44 -040067
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040068 // Setup 404 error handling
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050069 this.app.use((_req, res) => {
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040070 res.sendStatus(HttpStatusCode.NotFound);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040071 });
72
73 // Setup internal error handling
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050074 this.app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040075 log.error(err);
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040076 res.status(HttpStatusCode.InternalServerError).send(err.message);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040077 });
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040078 }
79}