blob: 16e9f12f62203874819048146b646bc6061c931d [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-Raynauldbfed1732022-11-01 20:49:35 -040027import { contactsRouter } from './routers/contacts-router.js';
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040028import { conversationRouter } from './routers/conversation-router.js';
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040029import { nameserverRouter } from './routers/nameserver-router.js';
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040030
31@Service()
Issam E. Maghnif796a092022-10-09 20:25:26 +000032export class App {
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040033 async build() {
34 const app = express();
35
Misha Krieger-Raynauld8ef48402022-10-23 21:41:51 -040036 // Setup middleware
37 app.use(helmet());
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040038 app.use(cors());
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040039 app.use(json());
40
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040041 // Setup routing
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040042 app.use('/auth', authRouter);
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040043 app.use('/account', accountRouter);
Misha Krieger-Raynauldbfed1732022-11-01 20:49:35 -040044 app.use('/contacts', contactsRouter);
Misha Krieger-Raynauld8a381da2022-11-03 17:37:51 -040045 // TODO: Moderator routes: https://git.jami.net/savoirfairelinux/jami-web/-/issues/93
46 app.use('/conversations', conversationRouter);
47 // TODO: Call routes: https://git.jami.net/savoirfairelinux/jami-web/-/issues/107
Misha Krieger-Raynauld6f9c7ae2022-10-28 11:41:45 -040048 app.use('/ns', nameserverRouter);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040049
50 // Setup 404 error handling
51 app.use((_req, res) => {
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040052 res.sendStatus(HttpStatusCode.NotFound);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040053 });
54
55 // Setup internal error handling
56 app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
57 log.error(err);
Misha Krieger-Raynauld2f5d1ce2022-10-23 21:13:33 -040058 res.status(HttpStatusCode.InternalServerError).send(err.message);
Misha Krieger-Raynauld708a9632022-10-14 22:55:59 -040059 });
60
61 return app;
62 }
63}