Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 1 | /* |
| 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 Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 18 | import cors from 'cors'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 19 | import express, { json, NextFunction, Request, Response } from 'express'; |
Misha Krieger-Raynauld | 8ef4840 | 2022-10-23 21:41:51 -0400 | [diff] [blame] | 20 | import helmet from 'helmet'; |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 21 | import { HttpStatusCode } from 'jami-web-common'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 22 | import log from 'loglevel'; |
| 23 | import { Service } from 'typedi'; |
| 24 | |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 25 | import { bindWebRTCCallbacks } from './handlers/webrtc-handler.js'; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 26 | import { checkAdminSetup } from './middleware/setup.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 27 | import { accountRouter } from './routers/account-router.js'; |
| 28 | import { authRouter } from './routers/auth-router.js'; |
Misha Krieger-Raynauld | 7f95933 | 2022-11-04 15:12:53 -0400 | [diff] [blame] | 29 | import { callRouter } from './routers/call-router.js'; |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 30 | import { contactsRouter } from './routers/contacts-router.js'; |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 31 | import { conversationRouter } from './routers/conversation-router.js'; |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 32 | import { defaultModeratorsRouter } from './routers/default-moderators-router.js'; |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 33 | import { nameserverRouter } from './routers/nameserver-router.js'; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 34 | import { setupRouter } from './routers/setup-router.js'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 35 | |
| 36 | @Service() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 37 | export class App { |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 38 | async build() { |
| 39 | const app = express(); |
| 40 | |
Misha Krieger-Raynauld | 8ef4840 | 2022-10-23 21:41:51 -0400 | [diff] [blame] | 41 | // Setup middleware |
| 42 | app.use(helmet()); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 43 | app.use(cors()); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 44 | app.use(json()); |
| 45 | |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 46 | // Enforce admin setup |
| 47 | app.use('/setup', setupRouter); |
| 48 | app.use(checkAdminSetup); |
| 49 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 50 | // Setup routing |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 51 | app.use('/auth', authRouter); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 52 | app.use('/account', accountRouter); |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame] | 53 | app.use('/contacts', contactsRouter); |
Misha Krieger-Raynauld | 153a148 | 2022-11-05 12:00:41 -0400 | [diff] [blame] | 54 | app.use('/default-moderators', defaultModeratorsRouter); |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 55 | app.use('/conversations', conversationRouter); |
Misha Krieger-Raynauld | 7f95933 | 2022-11-04 15:12:53 -0400 | [diff] [blame] | 56 | app.use('/calls', callRouter); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 57 | app.use('/ns', nameserverRouter); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 58 | |
Charlie | 2bc0d67 | 2022-11-04 11:53:44 -0400 | [diff] [blame] | 59 | // Setup WebSocket callbacks |
| 60 | bindWebRTCCallbacks(); |
| 61 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 62 | // Setup 404 error handling |
| 63 | app.use((_req, res) => { |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 64 | res.sendStatus(HttpStatusCode.NotFound); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 65 | }); |
| 66 | |
| 67 | // Setup internal error handling |
| 68 | app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => { |
| 69 | log.error(err); |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 70 | res.status(HttpStatusCode.InternalServerError).send(err.message); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 71 | }); |
| 72 | |
| 73 | return app; |
| 74 | } |
| 75 | } |