-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from CS3219-AY2425S1/development
Milestone D4
- Loading branch information
Showing
106 changed files
with
11,714 additions
and
572 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
cd ./frontend && npm run lint && npm run test | ||
cd .. | ||
cd ./frontend | ||
npm run lint | ||
npm run test -- --maxWorkers=50% | ||
cd .. | ||
|
||
cd ./backend/user-service && npm run lint && npm run test | ||
cd ../.. | ||
cd ./backend/user-service | ||
npm run lint | ||
npm run test -- --maxWorkers=50% | ||
cd ../.. | ||
|
||
cd ./backend/question-service && npm run lint && npm run test | ||
cd ./backend/question-service | ||
npm run lint | ||
npm run test -- --maxWorkers=50% | ||
cd ../.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
coverage | ||
node_modules | ||
tests | ||
.env* | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
NODE_ENV=development | ||
SERVICE_PORT=3002 | ||
|
||
ORIGINS=http://localhost:5173,http://127.0.0.1:5173 | ||
|
||
|
||
## FOR RABBITMQ, comment out the variables under whichever use case (1) or (2) that is not applicable | ||
# (1) RabbitMq for running matching service individually | ||
RABBITMQ_ADDR=amqp://localhost:5672 #comment out if use case is (2) | ||
|
||
# (2) RabbitMq for running matching service with other services using docker compose | ||
RABBITMQ_DEFAULT_USER=admin #comment out if use case is (1) | ||
RABBITMQ_DEFAULT_PASS=password #comment out if use case is (1) | ||
RABBITMQ_ADDR=amqp://admin:password@rabbitmq:5672 #comment out if use case is (1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:20-alpine | ||
|
||
WORKDIR /matching-service | ||
|
||
COPY package*.json ./ | ||
|
||
RUN npm ci | ||
|
||
COPY . . | ||
|
||
EXPOSE 3002 | ||
|
||
CMD ["npm", "run", "dev"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Matching Service Guide | ||
|
||
> Please ensure that you have completed the backend set-up [here](../README.md) before proceeding. | ||
## Setting-up Matching Service | ||
|
||
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. | ||
|
||
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`. 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. | ||
|
||
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. | ||
|
||
## Running Matching Service Individually with Docker | ||
|
||
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`. | ||
|
||
2. Open Command Line/Terminal and navigate into the `matching-service` directory. | ||
|
||
3. Run the command: `npm install`. This will install all the necessary dependencies. | ||
|
||
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. If you encounter connection errors, please wait for a few minutes before running `npm start` again as RabbitMq may take some time to start up. | ||
|
||
## After running | ||
|
||
1. To view Matching Service documentation, go to http://localhost:3002/docs. | ||
|
||
2. Using applications like Postman, you can interact with the Matching Service on port 3002. If you wish to change this, please update the `.env` file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import express, { Request, Response } from "express"; | ||
import dotenv from "dotenv"; | ||
import swaggerUi from "swagger-ui-express"; | ||
import yaml from "yaml"; | ||
import fs from "fs"; | ||
import cors from "cors"; | ||
|
||
import matchingRoutes from "./src/routes/matchingRoutes.ts"; | ||
|
||
dotenv.config(); | ||
|
||
export const allowedOrigins = process.env.ORIGINS | ||
? process.env.ORIGINS.split(",") | ||
: ["http://localhost:5173", "http://127.0.0.1:5173"]; | ||
|
||
const file = fs.readFileSync("./swagger.yml", "utf-8"); | ||
const swaggerDocument = yaml.parse(file); | ||
|
||
const app = express(); | ||
|
||
app.use(cors({ origin: allowedOrigins, credentials: true })); | ||
app.options("*", cors({ origin: allowedOrigins, credentials: true })); | ||
|
||
app.use("/api/matching", matchingRoutes); | ||
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | ||
app.get("/", (req: Request, res: Response) => { | ||
res.status(200).json({ message: "Hello world from matching service" }); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import amqplib, { Connection } from "amqplib"; | ||
import dotenv from "dotenv"; | ||
import { matchUsers } from "../src/utils/mq_utils"; | ||
import { MatchRequestItem } from "../src/handlers/matchHandler"; | ||
import { Complexities, Categories, Languages } from "../src/utils/constants"; | ||
|
||
dotenv.config(); | ||
|
||
let mrConnection: Connection; | ||
const queues: string[] = []; | ||
const pendingQueueRequests = new Map<string, Map<string, MatchRequestItem>>(); | ||
|
||
const initQueueNames = () => { | ||
for (const complexity of Object.values(Complexities)) { | ||
for (const category of Object.values(Categories)) { | ||
for (const language of Object.values(Languages)) { | ||
queues.push(`${complexity}_${category}_${language}`); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const setUpQueue = async (queueName: string) => { | ||
const consumerChannel = await mrConnection.createChannel(); | ||
await consumerChannel.assertQueue(queueName); | ||
|
||
consumerChannel.consume(queueName, (msg) => { | ||
if (msg !== null) { | ||
matchUsers(queueName, msg.content.toString()); | ||
consumerChannel.ack(msg); | ||
} | ||
}); | ||
}; | ||
|
||
export const connectToRabbitMq = async () => { | ||
try { | ||
initQueueNames(); | ||
mrConnection = await amqplib.connect(`${process.env.RABBITMQ_ADDR}`); | ||
for (const queue of queues) { | ||
await setUpQueue(queue); | ||
pendingQueueRequests.set(queue, new Map<string, MatchRequestItem>()); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export const sendToQueue = async ( | ||
complexity: string, | ||
category: string, | ||
language: string, | ||
data: MatchRequestItem | ||
): Promise<boolean> => { | ||
try { | ||
const queueName = `${complexity}_${category}_${language}`; | ||
const senderChannel = await mrConnection.createChannel(); | ||
senderChannel.sendToQueue(queueName, Buffer.from(JSON.stringify(data))); | ||
return true; | ||
} catch (error) { | ||
console.log(error); | ||
return false; | ||
} | ||
}; | ||
|
||
export const getPendingRequests = ( | ||
queueName: string | ||
): Map<string, MatchRequestItem> => { | ||
return pendingQueueRequests.get(queueName)!; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import globals from "globals"; | ||
import pluginJs from "@eslint/js"; | ||
import tseslint from "typescript-eslint"; | ||
|
||
export default [ | ||
{ files: ["**/*.{js,mjs,cjs,ts}"] }, | ||
{ languageOptions: { globals: globals.node } }, | ||
{ | ||
rules: { | ||
"@typescript-eslint/no-unused-vars": [ | ||
"warn", | ||
{ | ||
argsIgnorePattern: "^_", | ||
varsIgnorePattern: "^_", | ||
caughtErrorsIgnorePattern: "^_", | ||
}, | ||
], | ||
}, | ||
}, | ||
pluginJs.configs.recommended, | ||
...tseslint.configs.recommended, | ||
]; |
Oops, something went wrong.