Skip to content

Commit f201589

Browse files
authored
Feat: Docker files (#7)
1 parent 78411dd commit f201589

File tree

16 files changed

+682
-6539
lines changed

16 files changed

+682
-6539
lines changed

.env

-1
This file was deleted.

.env.example

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
SECRET_KEY = "secret_key_value"

Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM node:21.1.0-alpine
2+
3+
WORKDIR /app/
4+
COPY package*.json .babelrc ./
5+
6+
RUN npm install
7+
COPY ./app ./app
8+
RUN npm run build
9+
10+
EXPOSE 3001
11+
12+
CMD [ "node", "dist/index.js"]

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,30 @@ A Microservice API based on the hexagonal architecture using NodeJS + ExpressJS
3939
```zsh
4040
npm install
4141
```
42+
4243
### Before running the project
4344
```zsh
4445
cp .env.example .env
4546
```
4647

47-
#### Run
48+
#### Run the app using Docker
49+
50+
Use the following commands to run the app
51+
52+
```zsh
53+
docker build . -t ca-microservices-nodejs
54+
```
55+
56+
Then:
57+
58+
```zsh
59+
docker-compose up -d
60+
```
61+
62+
Now you can do a HTTP request to:
4863

4964
```zsh
50-
npm run start
65+
http://localhost:3001/users/123123123
5166
```
5267

5368
#### Test

app/internal/adapters/auth/jwt.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ const generateToken = (admin_id, expires_in) => {
55
return jsonwebtoken.sign({id: admin_id}, defaultConfig.security_key, {expiresIn:expires_in});
66
}
77

8-
export const VerifyToken = (req, res, next) => {
9-
const token = req.headers["authorization"] && req.headers["authorization"].split(" ")[1];
10-
if (token !== "undefined"){
11-
try {
12-
jsonwebtoken.verify(token, defaultConfig.security_key);
13-
next();
14-
} catch(err){
15-
next(err);
16-
}
17-
}else{
18-
next();
19-
}
8+
const verifyToken = (token) => {
9+
return jsonwebtoken.verify(token, defaultConfig.security_key);
2010
};
2111

2212
export const Token ={
2313
generateToken,
14+
verifyToken
2415
};

app/internal/adapters/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { MainRoute, UsersRoute, AuthRoute} from './routes';
1+
import { MainRoute, UsersRoute, AuthRoute } from './routes';
22

33
export const Adapters = {
4-
Routes: { MainRoute, UsersRoute, AuthRoute},
4+
Routes: { MainRoute, UsersRoute, AuthRoute },
55
};

app/internal/core/middlewares/auth.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Token } from '../../adapters/auth';
2+
3+
const tokenPaths =["/health/", "/users/"]
4+
5+
export const tokenValidator = (req, res, next) => {
6+
const token = req.headers["authorization"] && req.headers["authorization"].split(" ")[1];
7+
if (token !== "undefined" && tokenPaths.includes(req.path.slice(0,7))){
8+
try {
9+
Token.verifyToken(token);
10+
next();
11+
}catch(err){
12+
next(err);
13+
}
14+
}else{
15+
next();
16+
}
17+
};
18+

app/internal/core/middlewares/configurations.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { defaultConfig } from '../providers';
33
export const configurations = (req, res, next) => {
44
req.config = defaultConfig;
55
next();
6-
};
6+
};
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './logs';
22
export * from './error';
33
export * from './configurations';
4+
export * from './auth';
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dotenv/config';
2-
31
export const defaultConfig = {
42
security_key: process.env.SECRET_KEY,
53
};

app/internal/core/services/main.services.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
* Get health check response.
88
* @returns {import('../domain/models/responses.models.js').SimpleResponse} The health check response.
99
*/
10-
const getHealthCheck = () =>
11-
Responses.simple(`OK ${process.env.npm_package_version}`);
10+
const getHealthCheck = () => Responses.simple(`OK`);
1211

1312
export const MainServices = { getHealthCheck };

app/server/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import bodyParser from 'body-parser';
22
import cors from 'cors';
33
import express from 'express';
44
import { Adapters } from '../internal/adapters';
5-
import { VerifyToken } from '../internal/adapters/auth';
6-
import { Logger, HandlerError, configurations } from '../internal/core/middlewares';
5+
import {
6+
Logger,
7+
HandlerError,
8+
configurations,
9+
tokenValidator,
10+
} from '../internal/core/middlewares';
711

812
const { Routes } = Adapters;
913

@@ -13,10 +17,10 @@ App.use(bodyParser.json());
1317
App.use(bodyParser.urlencoded({ extended: false }));
1418
App.use(Logger);
1519
App.use(configurations);
16-
App.use(VerifyToken);
20+
App.use(tokenValidator);
1721

18-
Routes.MainRoute(App);
1922
Routes.AuthRoute(App);
23+
Routes.MainRoute(App);
2024
Routes.UsersRoute(App);
2125

2226
App.use(HandlerError);

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.4"
2+
services:
3+
express:
4+
image: ca-microservices-nodejs:latest
5+
container_name: ca-microservices-app
6+
volumes:
7+
- ./app:/usr/src/app
8+
ports:
9+
- "3001:3001"
10+
environment:
11+
- NODE_ENV=development
12+
- PORT=3001
13+
- SECRET_KEY=secret_key_value

0 commit comments

Comments
 (0)