Skip to content

Commit

Permalink
Merge pull request #36 from CS3219-AY2425S1/PEER-118,256-Match-Collab…
Browse files Browse the repository at this point in the history
…-Svc

[PEER-118][PEER-256] Add Matching Svc, Dockerize Collab Svc
  • Loading branch information
SeeuSim authored Oct 6, 2024
2 parents 4824c96 + 361d122 commit 375143e
Show file tree
Hide file tree
Showing 48 changed files with 328 additions and 529 deletions.
22 changes: 8 additions & 14 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
USER_PGDATA="/data/user-db"
USER_EXPRESS_DB_PORT=5431

QUESTION_PGDATA="/data/qn-db"
QUESTION_EXPRESS_DB_PORT=5433

COLLAB_PGDATA="/data/collab-db"
COLLAB_EXPRESS_DB_PORT=5434

MATCHING_PGDATA="/data/collab-db"
MATCHING_EXPRESS_DB_PORT=5434

USER_SERVICE_NAME=user-express
USER_EXPRESS_DB_PORT=5431
USER_EXPRESS_ENV=compose
USER_EXPRESS_PORT=9001
USER_PGDATA="/data/user-db"

QUESTION_SERVICE_NAME=question-express
QUESTION_EXPRESS_ENV=compose
QUESTION_EXPRESS_PORT=9002
QUESTION_EXPRESS_ENV=compose
QUESTION_EXPRESS_DB_PORT=5433
QUESTION_PGDATA="/data/qn-db"

COLLAB_CONTAINER_NAME=collab-express
COLLAB_SERVICE_NAME=collab-express
COLLAB_EXPRESS_ENV=compose
COLLAB_EXPRESS_PORT=9003

MATCHING_SERVICE_NAME=match-express
MATCHING_EXPRESS_ENV=compose
MATCHING_EXPRESS_PORT=9004

FRONTEND_SERVICE_NAME=frontend
FRONTEND_ENV=compose
FRONTEND_PORT=3000
4 changes: 4 additions & 0 deletions backend/collaboration/.env.compose
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# To be injected by Docker Compose
# PEERPREP_UI_HOST="http://frontend:3000"

EXPRESS_PORT=9003
3 changes: 3 additions & 0 deletions backend/collaboration/.env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PEERPREP_UI_HOST="http://host.docker.internal:5173"

EXPRESS_PORT=9003
10 changes: 3 additions & 7 deletions backend/collaboration/.env.local
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
EXPRESS_ENV="local"
EXPRESS_DB_HOST="localhost"
EXPRESS_DB_PORT=5434
POSTGRES_DB="collab"
POSTGRES_USER="peerprep-collab-express"
POSTGRES_PASSWORD="/86awM+Izo6949YgEQIls8HU+j5RlFYEInRy8auiNa8="
PGDATA="/data/collab-db"
PEERPREP_UI_HOST="http://localhost:5173"

EXPRESS_PORT=9003
141 changes: 1 addition & 140 deletions backend/collaboration/README.md
Original file line number Diff line number Diff line change
@@ -1,140 +1 @@
# Template Service

This directory contains the code for the Template
Service.

## Database

We use:

- PostgreSQL 16 for the database. To run it, we use:
- Docker to run the database, as well as inject any user-defined
configurations or SQL files into the Docker image.
- Docker-Compose to run the database, as well as any other
services this API microservice may depend on.
- [**Drizzle**](https://orm.drizzle.team/) for the ORM.

Follow the instructions below for the setup, as well as to learn how to work with the database.

### Setup

1. Install Docker Desktop on your device. Launch it.

2. To verify that it is launched and installed correctly, run the
following in your terminal:

```bash
docker --version
```

If the command does not error, and outputs a version, proceed to
the next step.

3. Inspect the `docker-compose.yml` file. It
should look like this:

```yml
services:
# ...
postgres:
# ...
volumes:
- "template-db-docker:/data/template-db"
# - ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5431:5432"
restart: unless-stopped

volumes:
template-db-docker:
external: true
```

We observe that this Database relies on a
Docker Volume. Replace all instances of
`template-db-docker` with your desired
volume name.

4. Then, create the Docker Volume with
the following command:

```bash
# in this case, the command is
# docker volume create template-db-docker
docker volume create <volume-name>
```
5. Finally, create the Database Container:

```bash
docker-compose up -d
```

6. To bring it down, run this command:

```bash
docker-compose down
```

### Schema

We maintain the schema in the `src/lib/db/schema.ts` file.

Refer to the Drizzle documentation to learn how
to properly define schemas. Then, insert your
schemas into the file.

### Migration

After you have created/updated your schemas in
the file, persist them to the Database with
Migrations.

1. Configure your credentials (port,
password, ...) in:

- `drizzle.config.ts`
- `drizzle.migrate.mts`.
- `src/lib/db/index.ts`.

In the future, we may wish to migrate these
credentials to environment variables.

2. Run the `npm run db:generate` command to
generate your `.sql` Migration Files under the
`drizzle` folder.

3. Rename your
`<migration_num>_<random_name>.sql` file
to `<migration_num>_<meaningful_name>.sql`.

For example:
- Generated: `0000_dazzling_squirrel.sql`
- Renamed: `0000_initial_schema.sql`.

Then, rename the
`meta/_journal.json` tag from
`0000_dazzling_squirrel` to
`0000_initial_schema` as well. Replace the
migration number and name with the one you
used.

4. Finally, run the migration with this:

```bash
npm run db:migrate
```

### Connecting with the DB

1. Import the `db` instance from `lib/db`.
2. Use the Drizzle APIs and the tables defined in
`src/lib/schema.ts` to interact with the
tables.

```ts
import { db, tableName } from '../lib/db';
const route = async (req, res) => {
await db.select().from(tableName); //...
}
```
# Collaboration Service
41 changes: 0 additions & 41 deletions backend/collaboration/docker-compose.yml

This file was deleted.

16 changes: 0 additions & 16 deletions backend/collaboration/drizzle.config.ts

This file was deleted.

4 changes: 0 additions & 4 deletions backend/collaboration/drizzle/0000_initial_schema.sql

This file was deleted.

41 changes: 0 additions & 41 deletions backend/collaboration/drizzle/meta/0000_snapshot.json

This file was deleted.

13 changes: 0 additions & 13 deletions backend/collaboration/drizzle/meta/_journal.json

This file was deleted.

5 changes: 5 additions & 0 deletions backend/collaboration/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

# To insert WS/RTC health checks

npm run start
12 changes: 7 additions & 5 deletions backend/collaboration/express.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ RUN npm run build
FROM node:lts-alpine AS production
WORKDIR /data/collab-express
COPY --from=build /data/collab-express/package*.json ./
RUN npm ci --omit=dev
COPY --from=build --chown=node:node /data/collab-express/dist ./dist

ARG env
COPY ".env.${env}" .
EXPOSE 8001
CMD [ "npm", "run", "start" ]
RUN npm ci --omit=dev

COPY entrypoint.sh .

ARG port
EXPOSE ${port}
ENTRYPOINT [ "/bin/sh", "entrypoint.sh" ]
17 changes: 6 additions & 11 deletions backend/collaboration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
"main": "dist/index.js",
"scripts": {
"dev": "env-cmd -f .env.local nodemon src/index.ts | pino-pretty",
"build": "env-cmd -f .env.local tsc && tsc-alias",
"start": "env-cmd -f .env.local node dist/index.js",
"build:prod": "env-cmd -f .env.prod tsc && tsc-alias",
"start:prod": "env-cmd -f .env.local node dist/index.js",
"db:generate": "env-cmd -f .env.local drizzle-kit generate",
"db:migrate": "env-cmd -f .env.local tsx ./src/lib/db/migrate.ts",
"db:inspect": "env-cmd -f .env.local drizzle-kit studio",
"build": "tsc && tsc-alias",
"start": "node dist/index.js",
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
"start:local": "env-cmd -f .env.local node dist/index.js",
"fmt": "prettier --config .prettierrc src --write",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -20,19 +17,17 @@
"description": "",
"dependencies": {
"cors": "^2.8.5",
"drizzle-orm": "^0.33.0",
"dotenv": "^16.4.5",
"env-cmd": "^10.1.0",
"express": "^4.21.0",
"http-status-codes": "^2.3.0",
"pino": "^9.4.0",
"pino-http": "^10.3.0",
"postgres": "^3.4.4"
"pino-http": "^10.3.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/node": "^22.5.5",
"drizzle-kit": "^0.24.2",
"nodemon": "^3.1.4",
"pino-pretty": "^11.2.2",
"ts-node": "^10.9.2",
Expand Down
Loading

0 comments on commit 375143e

Please sign in to comment.