Skip to content

Commit 450b3da

Browse files
authored
Merge pull request #40 from CS3219-AY2425S1/PEER-220-Matching-Logic
PEER-220: Matching Logic
2 parents 375143e + 3542d66 commit 450b3da

File tree

78 files changed

+9213
-5184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+9213
-5184
lines changed

.env.local

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
USER_SERVICE_NAME=user-express
2-
USER_EXPRESS_DB_PORT=5431
3-
USER_EXPRESS_ENV=compose
42
USER_EXPRESS_PORT=9001
3+
USER_EXPRESS_DB_PORT=5431
54
USER_PGDATA="/data/user-db"
65

76
QUESTION_SERVICE_NAME=question-express
87
QUESTION_EXPRESS_PORT=9002
9-
QUESTION_EXPRESS_ENV=compose
108
QUESTION_EXPRESS_DB_PORT=5433
119
QUESTION_PGDATA="/data/qn-db"
1210

1311
COLLAB_SERVICE_NAME=collab-express
14-
COLLAB_EXPRESS_ENV=compose
1512
COLLAB_EXPRESS_PORT=9003
1613

1714
MATCHING_SERVICE_NAME=match-express
18-
MATCHING_EXPRESS_ENV=compose
1915
MATCHING_EXPRESS_PORT=9004
16+
MATCHING_DB_USERNAME="peerprep-match-express"
17+
MATCHING_DB_PASSWORD="password"
18+
MATCHING_DB_HOST_PORT=6379
19+
MATCHING_DB_HOST_MGMT_PORT=3001
2020

2121
FRONTEND_SERVICE_NAME=frontend
22-
FRONTEND_ENV=compose
2322
FRONTEND_PORT=3000

backend/matching/.env.compose

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
# PEERPREP_UI_HOST="http://frontend:3000"
33

44
EXPRESS_PORT=9004
5+
6+
# MATCHING_DB_HOSTNAME="match-db"
7+
MATCHING_DB_PORT=6379
8+
9+
MATCHING_DB_USERNAME="peerprep-match-express"
10+
MATCHING_DB_PASSWORD="G7jBgyz9wGAFQ5La"
11+
REDIS_ARGS="--requirepass G7jBgyz9wGAFQ5La --user ${MATCHING_DB_USERNAME} on >G7jBgyz9wGAFQ5La ~* allcommands --user default off nopass nocommands"
12+

backend/matching/.env.docker

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
PEERPREP_UI_HOST="http://host.docker.internal:5173"
1+
PEERPREP_UI_HOST=http://host.docker.internal:5173
22

33
EXPRESS_PORT=9004
4+
5+
MATCHING_DB_HOSTNAME=host.docker.internal
6+
MATCHING_DB_PORT=6379
7+
8+
MATCHING_DB_USERNAME=peerprep-match-express
9+
MATCHING_DB_PASSWORD=G7jBgyz9wGAFQ5La

backend/matching/.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
PEERPREP_UI_HOST="http://localhost:5173"
22

33
EXPRESS_PORT=9004
4+
5+
MATCHING_DB_HOSTNAME="localhost"
6+
MATCHING_DB_PORT=6379
7+
8+
MATCHING_DB_USERNAME="peerprep-match-express"
9+
MATCHING_DB_PASSWORD="G7jBgyz9wGAFQ5La"
10+
REDIS_ARGS="--requirepass G7jBgyz9wGAFQ5La --user ${MATCHING_DB_USERNAME} on >G7jBgyz9wGAFQ5La ~* allcommands --user default off nopass nocommands"

backend/matching/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
# Matching Service
2+
3+
## Running with Docker (Standalone)
4+
5+
1. Run this command to build:
6+
```sh
7+
docker build \
8+
-t match-express-local \
9+
--build-arg port=9004 \
10+
-f express.Dockerfile .
11+
```
12+
2. Run this command, from the root folder:
13+
```sh
14+
make db-up
15+
```
16+
17+
3. Run the necessary migrate and seed commands, if you haven't yet.
18+
19+
4. Run this command to expose the container:
20+
```sh
21+
docker run -p 9004:9004 --env-file ./.env.docker match-express-local
22+
```
23+
5. To stop the process, use the Docker UI or CLI with `docker rm -f <container_id>` (The child process loop has issues terminating)
24+
25+
## Running with Docker-Compose (Main config)
26+
27+
Edit the variables in the `.env.compose` file and run `make up` from the root folder.
28+
29+
Any startup instructions will be run from `entrypoint.sh` instead.

backend/matching/entrypoint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/sh
22

3-
# To insert MQ health checks
3+
npm run db:seed:prod
4+
5+
rm -rf src tsconfig.json
6+
7+
npm uninstall tsx
48

59
npm run start

backend/matching/express.Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ COPY --from=build --chown=node:node /data/match-express/dist ./dist
1212

1313
RUN npm ci --omit=dev
1414

15+
COPY src/lib/db ./src/lib/db
16+
COPY src/lib/utils ./src/lib/utils
17+
COPY src/config.ts ./src
18+
COPY tsconfig.json .
1519
COPY entrypoint.sh .
1620

1721
ARG port

backend/matching/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"start": "node dist/index.js",
99
"build:local": "env-cmd -f .env.local tsc && tsc-alias",
1010
"start:local": "env-cmd -f .env.local node dist/index.js",
11+
"db:seed": "env-cmd -f .env.local tsx src/lib/db/seed.ts",
12+
"db:seed:prod": "tsx src/lib/db/seed.ts",
1113
"fmt": "prettier --config .prettierrc src --write",
1214
"test": "echo \"Error: no test specified\" && exit 1"
1315
},
@@ -16,22 +18,26 @@
1618
"license": "ISC",
1719
"description": "",
1820
"dependencies": {
21+
"async": "^3.2.6",
1922
"cors": "^2.8.5",
2023
"dotenv": "^16.4.5",
2124
"env-cmd": "^10.1.0",
2225
"express": "^4.21.0",
2326
"http-status-codes": "^2.3.0",
2427
"pino": "^9.4.0",
25-
"pino-http": "^10.3.0"
28+
"pino-http": "^10.3.0",
29+
"redis": "^4.7.0",
30+
"socket.io": "^4.8.0",
31+
"tsx": "^4.19.1"
2632
},
2733
"devDependencies": {
34+
"@types/async": "^3.2.24",
2835
"@types/cors": "^2.8.17",
2936
"@types/express": "^4.17.21",
3037
"@types/node": "^22.5.5",
3138
"nodemon": "^3.1.4",
3239
"pino-pretty": "^11.2.2",
3340
"ts-node": "^10.9.2",
34-
"tsc-alias": "^1.8.10",
35-
"tsx": "^4.19.1"
41+
"tsc-alias": "^1.8.10"
3642
}
3743
}

backend/matching/src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ import 'dotenv/config';
33
export const UI_HOST = process.env.PEERPREP_UI_HOST!;
44

55
export const EXPRESS_PORT = process.env.EXPRESS_PORT;
6+
7+
const DB_USERNAME = process.env.MATCHING_DB_USERNAME;
8+
const DB_PASSWORD = process.env.MATCHING_DB_PASSWORD;
9+
const DB_HOSTNAME = process.env.MATCHING_DB_HOSTNAME;
10+
const DB_PORT = Number.parseInt(process.env.MATCHING_DB_PORT ?? '6379');
11+
export const DB_URL = `redis://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOSTNAME}:${DB_PORT}`;
12+
// export const DB_URL = `redis://${DB_HOSTNAME}:${DB_PORT}`;
13+
14+
export const NODE_ENV = process.env.NODE_ENV;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Request, Response } from 'express';
2+
import { StatusCodes } from 'http-status-codes';
3+
4+
import { client } from '@/lib/db';
5+
import type { IRedisClient, IRequestMatchPayload } from '@/types';
6+
import { createNotifSocket, queueingService } from '@/services';
7+
8+
let redisClient: IRedisClient;
9+
export const matchRequestController = async (req: Request, res: Response) => {
10+
const payload: Partial<IRequestMatchPayload> = req.body;
11+
const { userId, difficulty, topic } = payload;
12+
if (!userId || (!difficulty && !topic)) {
13+
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
14+
}
15+
16+
if (!redisClient || !redisClient.isOpen || !redisClient.isReady) {
17+
redisClient = await client.connect();
18+
}
19+
20+
// TODO: Assign a proper socket to the user
21+
const socketRoom = createNotifSocket(userId);
22+
23+
// TODO: Test if room logic works and notif socket can connect
24+
// Send socket to user first for them to subscribe
25+
res
26+
.status(StatusCodes.OK)
27+
.json({
28+
socketPort: socketRoom,
29+
})
30+
.end();
31+
32+
// TODO: Wait for user to connect to notif socket, or add a time buffer
33+
34+
await queueingService(redisClient, { userId, difficulty, topic, socketPort: socketRoom });
35+
};

0 commit comments

Comments
 (0)