Skip to content

Commit 8f28162

Browse files
committed
Merge remote-tracking branch 'origin/PEER-255-Dockerize-DBs' into PEER-254-Dockerize-Frontend
2 parents 23cd50c + 684ba23 commit 8f28162

25 files changed

+355
-249
lines changed

.env.local

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,18 @@ QUESTION_EXPRESS_DB_PORT=5433
66

77
COLLAB_PGDATA="/data/collab-db"
88
COLLAB_EXPRESS_DB_PORT=5434
9+
10+
MATCHING_PGDATA="/data/collab-db"
11+
MATCHING_EXPRESS_DB_PORT=5434
12+
13+
USER_EXPRESS_ENV=compose
14+
USER_EXPRESS_PORT=9001
15+
16+
QUESTION_EXPRESS_ENV=compose
17+
QUESTION_EXPRESS_PORT=9002
18+
19+
COLLAB_EXPRESS_ENV=compose
20+
COLLAB_EXPRESS_PORT=9003
21+
22+
MATCHING_EXPRESS_ENV=compose
23+
MATCHING_EXPRESS_PORT=9003

Makefile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ migrate-seed:
77

88
db-up:
99
./scripts/ensure-volume.sh
10-
docker compose --env-file .env.local up -d
10+
docker compose --env-file .env.local -f docker-compose.local.yaml up -d
1111

1212
db-down:
13-
docker compose --env-file .env.local down
13+
docker compose --env-file .env.local -f docker-compose.local.yaml down
14+
15+
up:
16+
./scripts/ensure-volume.sh
17+
docker compose --env-file .env.local up -d
1418

19+
down:
20+
docker compose --env-file .env.local down

backend/user/.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
dist/
2+
dist/
3+
.git
4+
.env*

backend/user/.env.compose

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# When run with compose
2+
EXPRESS_ENV="compose"
3+
# to be changed
4+
PEERPREP_UI_HOST="http://frontend:3000"
5+
6+
EXPRESS_PORT=9001
7+
EXPRESS_DB_HOST="user-db"
8+
EXPRESS_DB_PORT=5432
9+
POSTGRES_DB="user"
10+
POSTGRES_USER="peerprep-user-express"
11+
POSTGRES_PASSWORD="69/X8JxtAVsM+0YHT4RR5D7Ahf7bTobI4EED64FrzIU="
12+
PGDATA="/data/user-db"
13+
14+
EXPRESS_JWT_SECRET_KEY="jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8="

backend/user/.env.docker

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
EXPRESS_ENV=local
2+
PEERPREP_UI_HOST=http://localhost:5173
3+
4+
EXPRESS_PORT=9001
5+
6+
# When run with standalone build
7+
EXPRESS_DB_HOST=host.docker.internal
8+
9+
EXPRESS_DB_PORT=5431
10+
POSTGRES_DB=user
11+
POSTGRES_USER=peerprep-user-express
12+
POSTGRES_PASSWORD=69/X8JxtAVsM+0YHT4RR5D7Ahf7bTobI4EED64FrzIU=
13+
PGDATA=/data/user-db
14+
15+
EXPRESS_JWT_SECRET_KEY=jd+9qlXA0a3YsmVf2KJgyiJ3SprIR318IAwhRXck4Y8=

backend/user/README.md

Lines changed: 18 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,28 @@
1-
# Template Service
1+
# User Service
22

3-
This directory contains the code for the Template
4-
Service.
3+
## Running with Docker (Standalone)
54

6-
## Database
7-
8-
We use:
9-
10-
- PostgreSQL 16 for the database. To run it, we use:
11-
- Docker to run the database, as well as inject any user-defined
12-
configurations or SQL files into the Docker image.
13-
- Docker-Compose to run the database, as well as any other
14-
services this API microservice may depend on.
15-
- [**Drizzle**](https://orm.drizzle.team/) for the ORM.
16-
17-
Follow the instructions below for the setup, as well as to learn how to work with the database.
18-
19-
### Setup
20-
21-
1. Install Docker Desktop on your device. Launch it.
22-
23-
2. To verify that it is launched and installed correctly, run the
24-
following in your terminal:
25-
26-
```bash
27-
docker --version
28-
```
29-
30-
If the command does not error, and outputs a version, proceed to
31-
the next step.
32-
33-
3. Inspect the `docker-compose.yml` file. It
34-
should look like this:
35-
36-
```yml
37-
services:
38-
# ...
39-
postgres:
40-
# ...
41-
volumes:
42-
- "template-db-docker:/data/template-db"
43-
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
44-
ports:
45-
- "5431:5432"
46-
restart: unless-stopped
47-
48-
volumes:
49-
template-db-docker:
50-
external: true
51-
```
52-
53-
We observe that this Database relies on a
54-
Docker Volume. Replace all instances of
55-
`template-db-docker` with your desired
56-
volume name.
57-
58-
4. Then, create the Docker Volume with
59-
the following command:
60-
61-
```bash
62-
# in this case, the command is
63-
# docker volume create template-db-docker
64-
docker volume create <volume-name>
65-
```
66-
5. Finally, create the Database Container:
67-
68-
```bash
69-
docker-compose up -d
5+
1. Run this command to build:
6+
```sh
7+
docker build \
8+
-t user-express-local \
9+
--build-arg port=9001 \
10+
-f express.Dockerfile .
7011
```
71-
72-
6. To bring it down, run this command:
73-
74-
```bash
75-
docker-compose down
12+
2. Run this command, from the root folder:
13+
```sh
14+
make db-up
7615
```
7716

78-
### Schema
79-
80-
We maintain the schema in the `src/lib/db/schema.ts` file.
81-
82-
Refer to the Drizzle documentation to learn how
83-
to properly define schemas. Then, insert your
84-
schemas into the file.
85-
86-
### Migration
87-
88-
After you have created/updated your schemas in
89-
the file, persist them to the Database with
90-
Migrations.
91-
92-
1. Configure your credentials (port,
93-
password, ...) in:
94-
95-
- `drizzle.config.ts`
96-
- `drizzle.migrate.mts`.
97-
- `src/lib/db/index.ts`.
17+
3. Run the necessary migrate and seed commands, if you haven't yet.
9818
99-
In the future, we may wish to migrate these
100-
credentials to environment variables.
101-
102-
2. Run the `npm run db:generate` command to
103-
generate your `.sql` Migration Files under the
104-
`drizzle` folder.
105-
106-
3. Rename your
107-
`<migration_num>_<random_name>.sql` file
108-
to `<migration_num>_<meaningful_name>.sql`.
109-
110-
For example:
111-
- Generated: `0000_dazzling_squirrel.sql`
112-
- Renamed: `0000_initial_schema.sql`.
113-
114-
Then, rename the
115-
`meta/_journal.json` tag from
116-
`0000_dazzling_squirrel` to
117-
`0000_initial_schema` as well. Replace the
118-
migration number and name with the one you
119-
used.
120-
121-
4. Finally, run the migration with this:
122-
123-
```bash
124-
npm run db:migrate
19+
4. Run this command to expose the container:
20+
```sh
21+
docker run -p 9001:9001 --env-file ./.env.docker user-express-local
12522
```
12623
127-
### Connecting with the DB
24+
## Running with Docker-Compose (Main config)
12825
129-
1. Import the `db` instance from `lib/db`.
130-
2. Use the Drizzle APIs and the tables defined in
131-
`src/lib/schema.ts` to interact with the
132-
tables.
26+
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
13327
134-
```ts
135-
import { db, tableName } from '../lib/db';
136-
137-
const route = async (req, res) => {
138-
await db.select().from(tableName); //...
139-
}
140-
```
28+
Any startup instructions will be run from `entrypoint.sh` instead.

backend/user/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ services:
2525
target: build
2626
args:
2727
# For building with the correct env vars
28-
- env=${EXPRESS_ENV}
2928
- port=${EXPRESS_PORT}
3029
ports:
3130
- "9001:${EXPRESS_PORT}"

backend/user/entrypoint.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
if [ ! -f .db-init ]; then
4+
echo "migrating..."
5+
npm run db:prod:migrate
6+
echo "seeding..."
7+
npm run db:prod:seed
8+
touch .db-init
9+
fi
10+
11+
npm run start

backend/user/express.Dockerfile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
FROM node:lts-alpine AS build
2-
WORKDIR /data/question-express
2+
WORKDIR /data/user-express
33
COPY package*.json ./
44
RUN npm install
5+
ARG env
56
COPY . .
67
RUN npm run build
78

89
FROM node:lts-alpine AS production
9-
WORKDIR /data/question-express
10-
COPY --from=build /data/question-express/package*.json ./
10+
WORKDIR /data/user-express
11+
COPY --from=build /data/user-express/package*.json ./
1112
RUN npm ci --omit=dev
12-
COPY --from=build --chown=node:node /data/question-express/dist ./dist
13+
COPY --from=build --chown=node:node /data/user-express/dist ./dist
1314

14-
ARG env
15-
COPY ".env.${env}" .
1615
ARG port
1716
EXPOSE ${port}
1817
CMD [ "npm", "run", "start" ]

backend/user/package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"main": "dist/index.js",
55
"scripts": {
66
"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",
7+
"build": "tsc && tsc-alias",
8+
"start": "node dist/index.js",
9+
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
10+
"start:local": "env-cmd -f .env.local node dist/index.js",
1111
"db:generate": "env-cmd -f .env.local drizzle-kit generate",
1212
"db:migrate": "env-cmd -f .env.local tsx ./src/lib/db/migrate.ts",
1313
"db:seed": "env-cmd -f .env.local tsx ./src/lib/db/seed.ts",
14+
"db:prod:migrate": "tsx ./src/lib/db/migrate.ts",
15+
"db:prod:seed": "tsx ./src/lib/db/seed.ts",
1416
"db:inspect": "env-cmd -f .env.local drizzle-kit studio",
1517
"fmt": "prettier --config .prettierrc src --write",
1618
"test": "echo \"Error: no test specified\" && exit 1"
@@ -23,6 +25,7 @@
2325
"bcrypt": "^5.1.1",
2426
"cookie-parser": "^1.4.6",
2527
"cors": "^2.8.5",
28+
"dotenv": "^16.4.5",
2629
"drizzle-orm": "^0.33.0",
2730
"env-cmd": "^10.1.0",
2831
"express": "^4.21.0",
@@ -32,7 +35,8 @@
3235
"jsonwebtoken": "^9.0.2",
3336
"pino": "^9.4.0",
3437
"pino-http": "^10.3.0",
35-
"postgres": "^3.4.4"
38+
"postgres": "^3.4.4",
39+
"tsx": "^4.19.1"
3640
},
3741
"devDependencies": {
3842
"@swc/core": "^1.7.26",

0 commit comments

Comments
 (0)