Skip to content
Merged
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
1 change: 0 additions & 1 deletion .claude/commands/code-review.md

This file was deleted.

12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
dist
coverage
logs
.git
.env.local
.env.test
*.test.ts
jest.config.js
eslint.config.mjs
start.sh
.claude
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Stage 1 — build
FROM node:22-alpine AS builder

RUN apk add --no-cache python3 make g++

WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

COPY tsconfig.json ./
COPY src ./src
RUN yarn build

# Stage 2 — production
FROM node:22-alpine

RUN apk add --no-cache python3 make g++

WORKDIR /app

COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production && apk del python3 make g++

RUN addgroup -S appgroup && adduser -S appuser -G appgroup

COPY --from=builder /app/dist ./dist
COPY migrations ./migrations

RUN mkdir -p logs && chown -R appuser:appgroup /app

USER appuser

EXPOSE 4000

CMD ["node", "dist/server.js"]
58 changes: 58 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
services:
db:
image: pgvector/pgvector:pg17
env_file: .env
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s
timeout: 5s
retries: 5

migrate:
build: .
env_file: .env
environment:
POSTGRES_HOST: db
command: ["node", "dist/database/migrations.js"]
networks:
- app-network
depends_on:
db:
condition: service_healthy
restart: "no"

app:
build: .
env_file: .env
environment:
POSTGRES_HOST: db
NODE_ENV: production
ports:
- "4000:4000"
networks:
- app-network
depends_on:
migrate:
condition: service_completed_successfully
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:4000/api/v1/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s

volumes:
pgdata:

networks:
app-network:
driver: bridge
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"tsup": {
"entry": [
"src/server.ts"
"src/server.ts",
"src/database/migrations.ts"
],
"sourcemap": true
},
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { RoleName } from "../roles/roles.validation";

export const TEST_ADMIN_USER = {
username: "admin",
email: "admin@example.com",
password: "password"
};

export const TEST_ROLES = new Map<RoleName, string>([
[RoleName.ADMIN, "Admin role"],
[RoleName.USER, "User role"]
]);
9 changes: 2 additions & 7 deletions src/__tests__/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { Client } from "pg";
import { migrate } from "../database/setup";
import bcrypt from "bcrypt";
import { RoleName } from "../roles/roles.validation";
import { TEST_ADMIN_USER } from "./constants";

const DEFAULT_ROLES = new Map<RoleName, string>([
[RoleName.ADMIN, "Admin role"],
[RoleName.USER, "User role"]
]);
import { TEST_ROLES, TEST_ADMIN_USER } from "./constants";

export default async function globalSetup() {
// Start the postgres container once for all tests
Expand All @@ -23,7 +18,7 @@ export default async function globalSetup() {
await migrate(client);

// Insert default roles
for (const [name, description] of DEFAULT_ROLES) {
for (const [name, description] of TEST_ROLES) {
await client.query(`INSERT INTO roles (name, description) VALUES ($1, $2)`, [
name,
description
Expand Down
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ class App {
});

this.initializeMiddlewares();
this.initializeHealthCheck();
this.initializeControllers(httpControllers);
this.initializeSocketHandlers(socketControllers);

this.app.use(errorMiddleware);
}

private initializeHealthCheck() {
this.app.get("/api/v1/health", (_req, res) => {
res.status(200).json({ status: "ok" });
});
}

private initializeMiddlewares() {
// Apply raw body parsing for Stripe webhook endpoint before JSON parsing
this.app.use("/api/v1/payments/orders", express.raw({ type: "application/json" }));
Expand Down
6 changes: 5 additions & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { config } from "dotenv";
import { cleanEnv, port, str, url } from "envalid";

config({ path: process.env.NODE_ENV === "test" ? ".env.test" : ".env.local" });
if (process.env.NODE_ENV === "test") {
config({ path: ".env.test" });
} else if (process.env.NODE_ENV !== "production") {
config({ path: ".env.local" });
}

export const env = cleanEnv(process.env, {
POSTGRES_USER: str(),
Expand Down