blob: f126ed49fc816279c0181ee5add4c2e34a342924 [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 */
18import express, { NextFunction, Request, Response } from 'express';
19import log from 'loglevel';
20import { Service } from 'typedi';
21
22import { StatusCode } from './constants.js';
23import { AuthRouter } from './routers/auth-router.js';
24
25@Service()
26class App {
27 constructor(private authRouter: AuthRouter) {}
28
29 async build() {
30 const app = express();
31
32 // Setup routing
33 const authRouter = await this.authRouter.build();
34 app.use('/auth', authRouter);
35
36 // Setup 404 error handling
37 app.use((_req, res) => {
38 res.sendStatus(StatusCode.NOT_FOUND);
39 });
40
41 // Setup internal error handling
42 app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => {
43 log.error(err);
44 res.status(StatusCode.INTERNAL_SERVER_ERROR).send(err.message);
45 });
46
47 return app;
48 }
49}
50
51export { App };