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 | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 19 | import log from 'loglevel'; |
| 20 | import { Service } from 'typedi'; |
| 21 | |
| 22 | import { StatusCode } from './constants.js'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame^] | 23 | import { accountRouter } from './routers/account-router.js'; |
| 24 | import { authRouter } from './routers/auth-router.js'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 25 | |
| 26 | @Service() |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 27 | export class App { |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 28 | async build() { |
| 29 | const app = express(); |
| 30 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame^] | 31 | app.use(json()); |
| 32 | |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 33 | // Setup routing |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 34 | app.use('/auth', authRouter); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame^] | 35 | app.use('/account', accountRouter); |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 36 | |
| 37 | // Setup 404 error handling |
| 38 | app.use((_req, res) => { |
| 39 | res.sendStatus(StatusCode.NOT_FOUND); |
| 40 | }); |
| 41 | |
| 42 | // Setup internal error handling |
| 43 | app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => { |
| 44 | log.error(err); |
| 45 | res.status(StatusCode.INTERNAL_SERVER_ERROR).send(err.message); |
| 46 | }); |
| 47 | |
| 48 | return app; |
| 49 | } |
| 50 | } |