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 | */ |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 18 | import express, { json, NextFunction, Request, Response } from 'express'; |
Misha Krieger-Raynauld | 8ef4840 | 2022-10-23 21:41:51 -0400 | [diff] [blame] | 19 | import helmet from 'helmet'; |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 20 | import { HttpStatusCode } from 'jami-web-common'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 21 | import log from 'loglevel'; |
| 22 | import { Service } from 'typedi'; |
| 23 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 24 | import { accountRouter } from './routers/account-router.js'; |
| 25 | import { authRouter } from './routers/auth-router.js'; |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame^] | 26 | import { contactsRouter } from './routers/contacts-router.js'; |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 27 | import { nameserverRouter } from './routers/nameserver-router.js'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 28 | |
| 29 | @Service() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 30 | export class App { |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 31 | async build() { |
| 32 | const app = express(); |
| 33 | |
Misha Krieger-Raynauld | 8ef4840 | 2022-10-23 21:41:51 -0400 | [diff] [blame] | 34 | // Setup middleware |
| 35 | app.use(helmet()); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 36 | app.use(json()); |
| 37 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 38 | // Setup routing |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 39 | app.use('/auth', authRouter); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 40 | app.use('/account', accountRouter); |
Misha Krieger-Raynauld | bfed173 | 2022-11-01 20:49:35 -0400 | [diff] [blame^] | 41 | app.use('/contacts', contactsRouter); |
Misha Krieger-Raynauld | 6f9c7ae | 2022-10-28 11:41:45 -0400 | [diff] [blame] | 42 | app.use('/ns', nameserverRouter); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 43 | |
| 44 | // Setup 404 error handling |
| 45 | app.use((_req, res) => { |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 46 | res.sendStatus(HttpStatusCode.NotFound); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | // Setup internal error handling |
| 50 | app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => { |
| 51 | log.error(err); |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 52 | res.status(HttpStatusCode.InternalServerError).send(err.message); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 53 | }); |
| 54 | |
| 55 | return app; |
| 56 | } |
| 57 | } |