|
1 | | -/* eslint-disable no-console */ |
2 | 1 | import express from 'express'; |
3 | | -import { rateLimit } from 'express-rate-limit'; |
| 2 | +import 'express-async-errors'; |
4 | 3 | import helmet from 'helmet'; |
5 | 4 | import morganBody from 'morgan-body'; |
6 | 5 | import Youch from 'youch'; |
7 | | -import routes from './routes'; |
| 6 | +import Routes from './routes/index.routes'; |
8 | 7 | import authServices from './services/auth.services'; |
| 8 | +import logService from './services/log.service'; |
9 | 9 | import constantUtil from './utils/constant.util'; |
10 | 10 | import dbUtil from './utils/db.util'; |
11 | 11 |
|
12 | | -const apiLimiter = rateLimit({ |
13 | | - windowMs: 1 * 60 * 1000, // 1 minutes |
14 | | - max: 120, |
15 | | - statusCode: 429, |
16 | | - message: constantUtil.MsgStatus429, |
17 | | -}); |
18 | | - |
19 | 12 | class App { |
20 | 13 | constructor() { |
21 | 14 | this.server = express(); |
22 | | - this.#port = process.env.NU_PORT; |
| 15 | + this.#port = constantUtil.NuPort; |
| 16 | + this.#routes = Routes; |
23 | 17 | this.#middlewares(); |
24 | | - this.#loadRoutes(); |
25 | | - this.#exceptionHandler(); |
26 | 18 | } |
27 | 19 |
|
| 20 | + #routes; |
| 21 | + |
28 | 22 | #port; |
29 | 23 |
|
30 | 24 | start() { |
31 | 25 | dbUtil.SQLite.authenticate() |
32 | 26 | .then(() => { |
33 | 27 | this.server.listen(this.#port, () => { |
34 | | - console.log(constantUtil.MsgStartAPI); |
| 28 | + logService.info(constantUtil.MsgStartAPI); |
35 | 29 | }); |
36 | 30 | }) |
37 | 31 | .catch((error) => { |
38 | | - console.error(constantUtil.MsgConnSQLiteError, error); |
39 | | - return error; |
| 32 | + logService.info(`Error message: ${error.message}`); |
| 33 | + logService.error({ method: 'start', error }).then(() => { |
| 34 | + return error; |
| 35 | + }); |
40 | 36 | }); |
41 | 37 | } |
42 | 38 |
|
43 | 39 | #middlewares() { |
44 | 40 | this.server.use(express.json()); |
45 | 41 | this.server.use(helmet()); |
46 | | - this.server.use('/', apiLimiter); |
| 42 | + this.server.set(constantUtil.TrustProxy, constantUtil.ExpressTrustProxyValue); |
| 43 | + |
47 | 44 | this.server.use(authServices.checkAuth); |
48 | | - if (process.env.MUST_RUN_MORGAN_BODY) { |
| 45 | + // As rotas devem ficar logo abaixo do authService |
| 46 | + // para que as requests sejam verificadas |
| 47 | + this.#routes.setupRoutes(this.server); |
| 48 | + |
| 49 | + if (constantUtil.MustRunMorganBody) { |
49 | 50 | morganBody(this.server); |
50 | 51 | } |
51 | | - } |
52 | | - |
53 | | - #loadRoutes() { |
54 | | - this.server.use(routes); |
| 52 | + this.#exceptionHandler(); |
55 | 53 | } |
56 | 54 |
|
57 | 55 | #exceptionHandler() { |
58 | 56 | this.server.use(async (err, request, response, next) => { |
59 | 57 | const errors = await new Youch(err, request).toJSON(); |
60 | | - return response.status(500).json(errors); |
| 58 | + logService.info(errors); |
| 59 | + logService.error({ method: 'exceptionHandler', error: err }).then(() => { |
| 60 | + return response.status(500).json({ messages: [constantUtil.MsgStatus500] }); |
| 61 | + }); |
61 | 62 | }); |
62 | 63 | } |
63 | 64 | } |
|
0 commit comments