Skip to content

Commit 7b9cb16

Browse files
committed
Merge development branch
1 parent 523d47c commit 7b9cb16

File tree

37 files changed

+743
-100
lines changed

37 files changed

+743
-100
lines changed

.husky/pre-commit

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
cd ./frontend && npm run lint && npm run test
2-
cd ..
1+
cd ./frontend
2+
npm run lint
3+
npm run test -- --maxWorkers=50%
4+
cd ..
35

4-
cd ./backend/user-service && npm run lint && npm run test
5-
cd ../..
6+
cd ./backend/user-service
7+
npm run lint
8+
npm run test -- --maxWorkers=50%
9+
cd ../..
610

7-
cd ./backend/question-service && npm run lint && npm run test
11+
cd ./backend/question-service
12+
npm run lint
13+
npm run test -- --maxWorkers=50%
14+
cd ../..
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coverage
2+
node_modules
3+
tests
4+
.env*
5+
*.md

backend/matching-service/.env.sample

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ NODE_ENV=development
22
PORT=3002
33

44
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
5+
6+
7+
## FOR RABBITMQ, comment out the variables under whichever use case (1) or (2) that is not applicable
8+
# (1) RabbitMq for running matching service individually
9+
RABBITMQ_ADDR=amqp://localhost:5672 #comment out if use case is (2)
10+
11+
# (2) RabbitMq for running matching service with other services using docker compose
12+
RABBITMQ_DEFAULT_USER=admin #comment out if use case is (1)
13+
RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1)
14+
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:20-alpine
2+
3+
WORKDIR /matching-service
4+
5+
COPY package*.json ./
6+
7+
RUN npm ci
8+
9+
COPY . .
10+
11+
EXPOSE 3002
12+
13+
CMD ["npm", "run", "dev"]

backend/matching-service/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
## Setting-up Matching Service
44

5-
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`.
5+
1. In the `matching-service` directory, create a copy of the `.env.sample` file and name it `.env`. If you are looking to run matching service with the other services using docker-compose, comment out the variable `RABBITMQ_ADDR` under use case (1) in the .env file. Otherwise, if you are looking to run matching service individually, comment out the variables `RABBITMQ_DEFAULT_USER`, `RABBITMQ_DEFAULT_PASS` and `RABBITMQ_ADDR` under use case (2) in the .env file.
66

7-
## Running Matching Service without Docker
7+
2. If you are running matching service together with other services using docker-compose, to set up credentials for RabbitMq, update the RabbitMq variables in the `.env` file. Update `RABBITMQ_DEFAULT_USER` and `RABBITMQ_DEFAULT_PASS` to what you want, then update `RABBITMQ_ADDR` to be `amqp://<RABBITMQ_DEFAULT_USER>:<RABBITMQ_DEFAULT_PASS>@rabbitmq:5672`.
8+
You can access RabbitMq management user interface locally with the username in `RABBITMQ_DEFAULT_USER` and password in `RABBITMQ_DEFAULT_PASS` at http://localhost:15672.
89

9-
1. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.
10+
3. If you are running matching service individually, you do not need to make any changes to `RABBITMQ_ADDR`. You can access RabbitMq management user interface locally with the username `guest` and password `guest` at http://localhost:15672.
1011

11-
2. Open Command Line/Terminal and navigate into the `matching-service` directory.
12+
## Running Matching Service Individually with Docker
1213

13-
3. Run the command: `npm install`. This will install all the necessary dependencies.
14+
1. Set up and run RabbitMq locally on your computer with the command `docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:4.0-management`.
1415

15-
4. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
16+
2. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.
17+
18+
3. Open Command Line/Terminal and navigate into the `matching-service` directory.
19+
20+
4. Run the command: `npm install`. This will install all the necessary dependencies.
21+
22+
5. Run the command `npm start` to start the Matching Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes.
1623

1724
## After running
1825

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import amqplib, { Connection } from "amqplib";
2+
import dotenv from "dotenv";
3+
import { matchUsers } from "../utils/mq_utils";
4+
5+
dotenv.config();
6+
7+
let mrConnection: Connection;
8+
const queue = "match_requests";
9+
10+
export const connectRabbitMq = async () => {
11+
try {
12+
mrConnection = await amqplib.connect(`${process.env.RABBITMQ_ADDR}`);
13+
const consumerChannel = await mrConnection.createChannel();
14+
await consumerChannel.assertQueue(queue);
15+
16+
consumerChannel.consume(queue, async (msg) => {
17+
if (msg !== null) {
18+
try {
19+
await matchUsers(msg.content.toString());
20+
} catch (error) {
21+
console.error(error);
22+
}
23+
consumerChannel.ack(msg);
24+
}
25+
});
26+
} catch (error) {
27+
console.error(error);
28+
process.exit(1);
29+
}
30+
};
31+
32+
type MatchRequestMessage = {
33+
userId: string;
34+
categories: string[] | string;
35+
complexities: string[] | string;
36+
sentTimestamp: number;
37+
ttlInSecs: number;
38+
};
39+
40+
export const sendRabbitMq = async (data: MatchRequestMessage) => {
41+
try {
42+
const senderChannel = await mrConnection.createChannel();
43+
senderChannel.sendToQueue(queue, Buffer.from(JSON.stringify(data)));
44+
} catch (error) {
45+
console.log(error);
46+
throw new Error("Failed to send match request");
47+
}
48+
};

backend/matching-service/package-lock.json

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/matching-service/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16+
"amqplib": "^0.10.4",
1617
"cors": "^2.8.5",
1718
"dotenv": "^16.4.5",
1819
"express": "^4.21.1",
@@ -23,6 +24,7 @@
2324
},
2425
"devDependencies": {
2526
"@eslint/js": "^9.12.0",
27+
"@types/amqplib": "^0.10.5",
2628
"@types/cors": "^2.8.17",
2729
"@types/express": "^5.0.0",
2830
"@types/jest": "^29.5.13",

0 commit comments

Comments
 (0)