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 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/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"] 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 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", 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 () => { 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) 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() } 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,