From 4d6e926e986f63dc2987809080cd646799f0100c Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Wed, 3 Jul 2024 09:19:54 -0300 Subject: [PATCH 1/8] fix: fix config of typeorm --- src/shared/config/typeorm.config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/config/typeorm.config.ts b/src/shared/config/typeorm.config.ts index 87fb3fa..b6fc3d3 100644 --- a/src/shared/config/typeorm.config.ts +++ b/src/shared/config/typeorm.config.ts @@ -15,8 +15,8 @@ const DatabaseConfig: DataSourceOptions = { synchronize: false, migrationsRun: true, logging: true, - entities: [`${__dirname}/../../../**/*.entity{.ts,.js}`], - migrations: [`${__dirname}/../../migrations/*{.ts, .js}`], + entities: [`${__dirname}/../../**/*.entity{.ts,.js}`], + migrations: [`${__dirname}/../../../migrations/*{.ts,.js}`], namingStrategy: new SnakeNamingStrategy() } From 722f1bf788f9f26e4502da544d561fc40da867eb Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Wed, 3 Jul 2024 09:22:04 -0300 Subject: [PATCH 2/8] chore: chage name of port in the env vars and create .env.example --- .env.example | 8 ++++++++ src/main.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c0a8f2f --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +# Server variables +SERVER_PORT=1234 + +# Database variables +DATABASE_PORT=2134 +DATABASE_USER=example_user +DATABASE_PASS=example_pass +DATABASE_NAME=example_name diff --git a/src/main.ts b/src/main.ts index 58736c0..547a26e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import BootstrapUser from '@user/user.bootstrap' import { type DataSource } from 'typeorm' dotenv.config() -const PORT = Number(process.env.PORT) +const PORT = Number(process.env.SERVER_PORT) /* Main */ ;(async () => { From c5a235846f160fc0fe34e447f7c34d73d828654a Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Wed, 3 Jul 2024 09:24:19 -0300 Subject: [PATCH 3/8] feat: create migration command to create, and enhance command to generate and run --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 8ef81e5..b7db557 100644 --- a/package.json +++ b/package.json @@ -19,9 +19,9 @@ "pre-check": "pnpm run ts-check && pnpm run prettier && pnpm run lint", "pre-commit": "lint-staged", "typeorm": "typeorm-ts-node-commonjs", - "migration:generate": "pnpm run typeorm migration:generate ./migrations/migration -d ./src/shared/config/typeorm.config.ts", - "migration:run": "pnpm run typeorm migration:run -d ./src/shared/config/typeorm.config.ts", - "migration:revert": "pnpm run typeorm migration:revert --config ./src/config/ormconfig.ts" + "migration:create": "pnpm run typeorm migration:create ./migrations/migration -o", + "migration:generate": "pnpm run typeorm migration:generate ./migrations/migration -d ./src/shared/config/typeorm.config.ts -o", + "migration:run": "pnpm run typeorm migration:run -d ./src/shared/config/typeorm.config.ts" }, "keywords": [], "author": "Figarillo", From f1b7cf57dcecab47d0e4fc9c2269ce5b7079b19f Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Wed, 3 Jul 2024 09:37:52 -0300 Subject: [PATCH 4/8] chore: remove ts rule, noImplicitReturns to simplify the code --- tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 341f0fe..aed5777 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -39,7 +39,6 @@ "strictPropertyInitialization": true, "noUnusedLocals": true, "noUnusedParameters": true, - "noImplicitReturns": true, "allowUnusedLabels": true, "allowUnreachableCode": true, From 5b3eef6646e8e4cf639efab44c32e65f9e9ec78b Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Thu, 4 Jul 2024 00:20:06 -0300 Subject: [PATCH 5/8] fix: change wrong env var in fastify config --- src/shared/config/fastify.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/config/fastify.config.ts b/src/shared/config/fastify.config.ts index 914c91b..599a303 100644 --- a/src/shared/config/fastify.config.ts +++ b/src/shared/config/fastify.config.ts @@ -15,7 +15,7 @@ class FastifyConifg { try { await this.server.listen({ host: '0.0.0.0', - port: parseInt(process.env.PORT ?? '5000') + port: parseInt(process.env.SERVER_PORT ?? '5000') }) } catch (err) { this.server.log.error(err) From 6fce01bd2a0d1315db2ecea48b5c33166cd7a5b9 Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Thu, 4 Jul 2024 00:21:20 -0300 Subject: [PATCH 6/8] feat: prepare docker compose to add Dockerfile --- docker-compose.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 9b797ad..88ab49c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,19 @@ services: + ### API SERVER ### + apiserver: + build: + context: . + dockerfile: Dockerfile + restart: always + depends_on: + - database + ports: + - '8080:8080' + environment: + DATABASE_HOST: database + networks: + - dev-network + ### DATABASE ### database: image: postgres:16.2-alpine3.19 @@ -11,7 +26,13 @@ services: - '${DATABASE_PORT}:5432' volumes: - pgdata:/var/lib/postgresql/data + networks: + - dev-network volumes: pgdata: driver: local + +networks: + dev-network: + driver: bridge From 19c71478919f4056e11c248f8ca246907037862c Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Thu, 4 Jul 2024 00:22:00 -0300 Subject: [PATCH 7/8] feat: add .dockerignore --- .dockerignore | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e739d9e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +node_modules +dist +.git +.gitignore +.DS_Store +README.md +.dockerignore +.husky +commitlint.config.js +notes +.prettier* +.eslint* +.vscode From 6ca59cad228f07eb1982d2ae2c04a4f46546ad59 Mon Sep 17 00:00:00 2001 From: Figaarillo Date: Thu, 4 Jul 2024 00:22:30 -0300 Subject: [PATCH 8/8] feat: create Dockerfile --- Dockerfile | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..405778a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +# Stage 1: Build +FROM node:20.15.0-alpine3.19 AS builder + +# Set the working directory +WORKDIR /usr/src/app + +# Copy package.json and pnpm-lock.yaml into the container +COPY package.json pnpm-lock.yaml ./ + +# Install pnpm to install dependencies +RUN npm install -g pnpm +RUN pnpm install + +# Copy the rest of the source code +COPY . . + +# Generate the migrations +RUN pnpm run migration:generate +RUN pnpm run migration:run + +# Build the project +RUN pnpm build + +# Stage 2: Production +FROM node:20.15.0-alpine3.19 as production + +# Set the working directory +WORKDIR /usr/src/app + +# Copy package.json and pnpm-lock.yaml into the container +COPY package.json pnpm-lock.yaml ./ + +# Install pnpm to install production dependencies +RUN npm install -g pnpm +RUN pnpm install + +# Copy the built code and necessary files from the builder stage +COPY --from=builder /usr/src/app/dist ./dist +COPY --from=builder /usr/src/app/migrations ./migrations +COPY --from=builder /usr/src/app/.env ./ + +ENV NODE_ENV=production + +EXPOSE 8080 + +CMD ["node", "dist/main.js"]