Skip to content

Commit f514edf

Browse files
committed
chore(docker): Migrate configs to env file
Signed-off-by: SeeuSim <[email protected]>
1 parent df8790a commit f514edf

File tree

12 files changed

+259
-59
lines changed

12 files changed

+259
-59
lines changed

backend/collaboration/.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
EXPRESS_ENV="local"
2+
EXPRESS_DB_HOST="localhost"
3+
EXPRESS_DB_PORT=5434
4+
POSTGRES_DB="collaboration"
5+
POSTGRES_USER="peerprep-collab-express"
6+
POSTGRES_PASSWORD="/86awM+Izo6949YgEQIls8HU+j5RlFYEInRy8auiNa8="
7+
PGDATA="/data/collab-db"
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1+
# Command: docker-compose --env-file .env.local up -d
2+
13
services:
2-
postgres:
3-
hostname: postgres
4-
image: postgres:16.4
5-
container_name: 'collaboration_db'
4+
collab-db:
5+
hostname: "collab-db"
6+
image: "postgres:16.4"
7+
container_name: "collab-db"
68
build:
79
context: ./src/lib/db
8-
environment:
9-
POSTGRES_DB: 'Collaboration'
10-
POSTGRES_USER: 'user'
11-
POSTGRES_PASSWORD: 'user'
12-
PGDATA: '/data/collaboration-db'
10+
env_file:
11+
- ./.env.local
1312
volumes:
14-
- 'collaboration-db-docker:/data/collaboration-db'
13+
- "collab-db-docker:/${PGDATA}"
1514
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
1615
ports:
17-
- '5434:5432'
16+
- "${EXPRESS_DB_PORT}:5432"
1817
restart: unless-stopped
1918

20-
express:
21-
image: 'collaboration-express'
22-
container_name: 'collaboration-express'
19+
collab-express:
20+
image: 'collab-express'
21+
container_name: 'collab-express'
2322
build:
2423
context: ./
2524
dockerfile: ./express.Dockerfile
2625
target: build
26+
args:
27+
# For building with the correct env vars
28+
- env=${EXPRESS_ENV}
2729
ports:
2830
- '9003:8001'
2931
command: node dist/index.js
32+
environment:
33+
# Docker Compose Specific
34+
- EXPRESS_DB_HOST=collab-db
35+
- EXPRESS_DB_PORT=5432
36+
# Env File vars
37+
- POSTGRES_DB=${POSTGRES_DB}
38+
- POSTGRES_USER=${POSTGRES_USER}
39+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
3040
depends_on:
31-
postgres:
32-
condition: service_started
33-
restart: true
41+
- collab-db
3442
volumes:
35-
collaboration-db-docker:
43+
collab-db-docker:
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { defineConfig } from 'drizzle-kit';
22

3+
const config = {
4+
host: process.env.EXPRESS_DB_HOST!,
5+
port: Number.parseInt(process.env.EXPRESS_DB_PORT!),
6+
database: process.env.POSTGRES_DB!,
7+
user: process.env.POSTGRES_USER,
8+
password: process.env.POSTGRES_PASSWORD,
9+
};
10+
311
export default defineConfig({
412
schema: './src/lib/db/schema.ts',
513
out: './drizzle',
614
dialect: 'postgresql',
7-
dbCredentials: {
8-
host: 'localhost',
9-
port: 5434,
10-
database: 'Collaboration',
11-
user: 'user',
12-
password: 'user',
13-
},
15+
dbCredentials: config,
1416
});

backend/collaboration/drizzle.migrate.mts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import postgres from 'postgres';
2+
import { drizzle } from 'drizzle-orm/postgres-js';
3+
import { migrate } from 'drizzle-orm/postgres-js/migrator';
4+
5+
const config = {
6+
host: process.env.EXPRESS_DB_HOST!,
7+
port: Number.parseInt(process.env.EXPRESS_DB_PORT!),
8+
database: process.env.POSTGRES_DB,
9+
user: process.env.POSTGRES_USER,
10+
password: process.env.POSTGRES_PASSWORD,
11+
};
12+
const migrationConnection = postgres({ ...config, max: 1 });
13+
14+
const db = drizzle(migrationConnection);
15+
16+
const main = async () => {
17+
await migrate(db, { migrationsFolder: 'drizzle' });
18+
await migrationConnection.end();
19+
};
20+
21+
void main();

backend/collaboration/express.Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ WORKDIR /data/user-express
1010
COPY --from=build /data/user-express/package*.json ./
1111
RUN npm ci --omit=dev
1212
COPY --from=build --chown=node:node /data/user-express/dist ./dist
13+
14+
ARG env
15+
COPY ".env.${env}" .
1316
EXPOSE 8001
14-
ENTRYPOINT ["node", "dist/index.js"]
17+
CMD [ "npm", "run", "start" ]

backend/collaboration/package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"version": "1.0.0",
44
"main": "dist/index.js",
55
"scripts": {
6-
"dev": "nodemon src/index.ts | pino-pretty",
7-
"build": "tsc",
8-
"start": "node dist/index.js",
6+
"dev": "env-cmd -f .env.local nodemon src/index.ts | pino-pretty",
7+
"build": "env-cmd -f .env.local tsc && tsc-alias",
8+
"start": "env-cmd -f .env.local node dist/index.js",
9+
"build:prod": "env-cmd -f .env.prod tsc && tsc-alias",
10+
"start:prod": "env-cmd -f .env.local node dist/index.js",
11+
"db:generate": "env-cmd -f .env.local drizzle-kit generate",
12+
"db:migrate": "env-cmd -f .env.local tsx drizzle.migrate.ts",
13+
"db:inspect": "env-cmd -f .env.local drizzle-kit studio",
914
"fmt": "prettier --config .prettierrc src --write",
10-
"db:generate": "drizzle-kit generate",
11-
"db:migrate": "tsx drizzle.migrate.mts",
1215
"test": "echo \"Error: no test specified\" && exit 1"
1316
},
1417
"keywords": [],
@@ -17,6 +20,7 @@
1720
"description": "",
1821
"dependencies": {
1922
"drizzle-orm": "^0.33.0",
23+
"env-cmd": "^10.1.0",
2024
"express": "^4.21.0",
2125
"pino": "^9.4.0",
2226
"pino-http": "^10.3.0",
@@ -28,6 +32,7 @@
2832
"drizzle-kit": "^0.24.2",
2933
"nodemon": "^3.1.4",
3034
"pino-pretty": "^11.2.2",
31-
"ts-node": "^10.9.2"
35+
"ts-node": "^10.9.2",
36+
"tsc-alias": "^1.8.10"
3237
}
3338
}

backend/collaboration/src/lib/db/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { drizzle } from 'drizzle-orm/postgres-js';
22
import postgres from 'postgres';
33

4-
const queryClient = postgres({
5-
host: 'localhost',
6-
port: 5434,
7-
database: 'Collaboration',
8-
user: 'user',
9-
password: 'user',
10-
});
4+
export const config = {
5+
host: process.env.EXPRESS_DB_HOST!,
6+
port: Number.parseInt(process.env.EXPRESS_DB_PORT!),
7+
database: process.env.POSTGRES_DB,
8+
user: process.env.POSTGRES_USER,
9+
password: process.env.POSTGRES_PASSWORD,
10+
};
11+
12+
const queryClient = postgres(config);
1113

1214
export const db = drizzle(queryClient);
1315

backend/collaboration/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import express, { json } from 'express';
44
import pino from 'pino-http';
55
import { sql } from 'drizzle-orm';
66

7-
import { db } from '@/lib/db';
7+
import { config, db } from '@/lib/db';
88
import { logger } from '@/lib/utils';
99

1010
const app = express();
@@ -24,6 +24,7 @@ export const dbHealthCheck = async () => {
2424
} catch (error) {
2525
const { message } = error as Error;
2626
logger.error('Cannot connect to DB: ' + message);
27+
logger.error(`DB Config: ${JSON.stringify({ ...config, password: '<REDACTED>' })}`);
2728
exit(1);
2829
}
2930
};

backend/question/.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
EXPRESS_ENV="local"
2+
EXPRESS_DB_HOST="localhost"
3+
EXPRESS_DB_PORT=5433
4+
POSTGRES_DB="question"
5+
POSTGRES_USER="peerprep-qn-express"
6+
POSTGRES_PASSWORD="Xk8qEcEI2sizjfEn/lF6mLqiyBECjIHY3q6sdXf9poQ="
7+
PGDATA="/data/qn-db"

0 commit comments

Comments
 (0)