blob: f7c860ad6e3e82a8628f2b47ef7ff6a08e6a0f64 [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
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040025import { accountRouter } from './routers/account-router.js';
26import { authRouter } from './routers/auth-router.js';
Misha Krieger-Raynauld7f959332022-11-04 15:12:53 -040027import { callRouter } from './routers/call-router.js';
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040028import { contactsRouter } from './routers/contacts-router.js';
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040029import { conversationRouter } from './routers/conversation-router.js';
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040030import { defaultModeratorsRouter } from './routers/default-moderators-router.js';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040031import { nameserverRouter } from './routers/nameserver-router.js';
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040032
33@Service()
Issam E. Maghnif796a092022-10-09 20:25:26 +000034export class App {
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040035 async build() {
36 const app = express();
37
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040038 // Setup middleware
39 app.use(helmet());
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040040 app.use(cors());
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040041 app.use(json());
42
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040043 // Setup routing
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040044 app.use('/auth', authRouter);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040045 app.use('/account', accountRouter);
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040046 app.use('/contacts', contactsRouter);
Misha Krieger-Raynauld153a1482022-11-05 12:00:41 -040047 app.use('/default-moderators', defaultModeratorsRouter);
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040048 app.use('/conversations', conversationRouter);
Misha Krieger-Raynauld7f959332022-11-04 15:12:53 -040049 app.use('/calls', callRouter);
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040050 app.use('/ns', nameserverRouter);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040051
52 // Setup 404 error handling
53 app.use((_req, res) => {
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040054 res.sendStatus(HttpStatusCode.NotFound);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040055 });
56
57 // Setup internal error handling
58 app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
59 log.error(err);
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040060 res.status(HttpStatusCode.InternalServerError).send(err.message);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040061 });
62
63 return app;
64 }
65}