Skip to content

Commit 920aa6e

Browse files
authored
Merge pull request #1 from tglima/v0.0.4
v0.4.0
2 parents 91ed71e + a137c29 commit 920aa6e

33 files changed

+3321
-598
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ dist
8888
# https://nextjs.org/blog/next-9-1#public-directory-support
8989
# public
9090

91+
# temp folder
92+
temp/
93+
9194
# vuepress build output
9295
.vuepress/dist
9396

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"type": "node-terminal",
1212
"cwd": "${workspaceFolder}/src",
13-
"envFile": "${workspaceFolder}/config.env",
13+
"envFile": "${workspaceFolder}/src/app/config/develop.env",
1414
"skipFiles": [
1515
"<node_internals>/**"
1616
],

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# restapi-node-docker
2-
API Rest desenvolvida com NodeJS e PostgreSQL para rodar dentro de um container docker
2+
3+
API Rest desenvolvida com NodeJS e SQLite para rodar dentro de um container docker

config.env

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ insert_final_newline = true
2626
#Define e forca como sera o estilo de chaves que sera usado
2727
#Restringe essa configuracao apenas para arquivos do tipo js
2828
[*.{js}]
29-
indent_brace_style = K&R
29+
indent_brace_style = K&R
30+
31+
[*.js]
32+
max_line_length = 110

src/.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"singleQuote": true,
3-
"trailingComma": "es5"
3+
"trailingComma": "es5",
4+
"printWidth": 110
45
}

src/Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Utiliz a imagem do Node.js 18 no Alpine
2+
FROM node:18-alpine
3+
4+
# Pré define o valor das variáveis de ambiente utilizadas pelas aplicaçaõ
5+
# Caso o usuário informe será utilizado o valor definido por ele
6+
7+
ARG NU_PORT=1985
8+
ARG NODE_ENV=homolog
9+
ARG EXPRESS_TRUST_PROXY_VALUE=0
10+
ARG SWAGGER_URL="http://localhost:{{NU_PORT}}/swagger"
11+
12+
# Define as variáveis de ambiente específicas para a aplicação
13+
ENV NU_PORT=$NU_PORT
14+
ENV NODE_ENV=$NODE_ENV
15+
ENV EXPRESS_TRUST_PROXY_VALUE=$EXPRESS_TRUST_PROXY_VALUE
16+
ENV SWAGGER_URL=$SWAGGER_URL
17+
18+
19+
# Define o diretório de trabalho dentro do container
20+
WORKDIR /usr/src/webapi
21+
22+
# Copie os arquivos da pasta 'dist' para o diretório de trabalho
23+
COPY . /usr/src/webapi
24+
25+
# Apaga o arquivo Dockerfile
26+
RUN rm /usr/src/webapi/Dockerfile
27+
28+
#Instala o nano caso precise editar algum arquivo pelo terminal
29+
RUN apk add --no-cache nano
30+
31+
# Atualiza o npm para uma versão mais atual
32+
RUN npm install -g [email protected]
33+
34+
# Instale apenas as dependências de produção do projeto
35+
RUN npm install --omit=dev --exact
36+
37+
# Exponha a porta configurada
38+
EXPOSE $NU_PORT
39+
40+
# Comando para iniciar a aplicação (substitua pelo seu comando de inicialização)
41+
CMD ["npm", "start"]

src/app/app.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,64 @@
1-
/* eslint-disable no-console */
21
import express from 'express';
3-
import { rateLimit } from 'express-rate-limit';
2+
import 'express-async-errors';
43
import helmet from 'helmet';
54
import morganBody from 'morgan-body';
65
import Youch from 'youch';
7-
import routes from './routes';
6+
import Routes from './routes/index.routes';
87
import authServices from './services/auth.services';
8+
import logService from './services/log.service';
99
import constantUtil from './utils/constant.util';
1010
import dbUtil from './utils/db.util';
1111

12-
const apiLimiter = rateLimit({
13-
windowMs: 1 * 60 * 1000, // 1 minutes
14-
max: 120,
15-
statusCode: 429,
16-
message: constantUtil.MsgStatus429,
17-
});
18-
1912
class App {
2013
constructor() {
2114
this.server = express();
22-
this.#port = process.env.NU_PORT;
15+
this.#port = constantUtil.NuPort;
16+
this.#routes = Routes;
2317
this.#middlewares();
24-
this.#loadRoutes();
25-
this.#exceptionHandler();
2618
}
2719

20+
#routes;
21+
2822
#port;
2923

3024
start() {
3125
dbUtil.SQLite.authenticate()
3226
.then(() => {
3327
this.server.listen(this.#port, () => {
34-
console.log(constantUtil.MsgStartAPI);
28+
logService.info(constantUtil.MsgStartAPI);
3529
});
3630
})
3731
.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+
});
4036
});
4137
}
4238

4339
#middlewares() {
4440
this.server.use(express.json());
4541
this.server.use(helmet());
46-
this.server.use('/', apiLimiter);
42+
this.server.set(constantUtil.TrustProxy, constantUtil.ExpressTrustProxyValue);
43+
4744
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) {
4950
morganBody(this.server);
5051
}
51-
}
52-
53-
#loadRoutes() {
54-
this.server.use(routes);
52+
this.#exceptionHandler();
5553
}
5654

5755
#exceptionHandler() {
5856
this.server.use(async (err, request, response, next) => {
5957
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+
});
6162
});
6263
}
6364
}

src/app/assets/database.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)