Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules
dist
.git
.gitignore
.DS_Store
README.md
.dockerignore
.husky
commitlint.config.js
notes
.prettier*
.eslint*
.vscode
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/config/fastify.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/shared/config/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"strictPropertyInitialization": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"allowUnusedLabels": true,
"allowUnreachableCode": true,

Expand Down