Skip to content

Commit

Permalink
Merge pull request #79 from CS3219-AY2425S1/development
Browse files Browse the repository at this point in the history
Milestone D4
  • Loading branch information
nicolelim02 authored Oct 20, 2024
2 parents c4539a3 + 14a2277 commit 4d178d4
Show file tree
Hide file tree
Showing 106 changed files with 11,714 additions and 572 deletions.
51 changes: 29 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,33 @@ on:

env:
NODE_VERSION: 20
FIREBASE_PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID }}
FIREBASE_PRIVATE_KEY: ${{ secrets.FIREBASE_PRIVATE_KEY }}
FIREBASE_CLIENT_EMAIL: ${{ secrets.FIREBASE_CLIENT_EMAIL }}
FIREBASE_STORAGE_BUCKET: ${{ secrets.FIREBASE_STORAGE_BUCKET }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}

permissions:
contents: read

jobs:
ci:
frontend-ci:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setting node version
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
working-directory: frontend
run: npm install
- name: Linting
working-directory: frontend
run: npm run lint
- name: Test
run: docker compose -f docker-compose-test.yml run --rm test-frontend
backend-ci:
runs-on: ubuntu-latest
strategy:
matrix:
service: [frontend, backend/question-service, backend/user-service]
service: [question-service, user-service, matching-service]
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -30,21 +42,16 @@ jobs:
with:
node-version: ${{ env.NODE_VERSION }}
- name: Install dependencies
working-directory: ${{ matrix.service }}
working-directory: backend/${{ matrix.service }}
run: npm install
- name: Linting
working-directory: ${{ matrix.service }}
working-directory: backend/${{ matrix.service }}
run: npm run lint
- name: Set .env variables
working-directory: ${{ matrix.service }}
run: |
touch .env
echo "FIREBASE_PROJECT_ID=${{ env.FIREBASE_PROJECT_ID }}" >> .env
echo "FIREBASE_PRIVATE_KEY=${{ env.FIREBASE_PRIVATE_KEY }}" >> .env
echo "FIREBASE_CLIENT_EMAIL=${{ env.FIREBASE_CLIENT_EMAIL }}" >> .env
echo "FIREBASE_CLIENT_EMAIL=${{ env.FIREBASE_CLIENT_EMAIL }}" >> .env
echo "FIREBASE_STORAGE_BUCKET=${{ env.FIREBASE_STORAGE_BUCKET }}" >> .env
echo "JWT_SECRET=${{ env.JWT_SECRET }}" >> .env
- name: Tests
working-directory: ${{ matrix.service }}
run: npm test
- name: Test
env:
FIREBASE_PROJECT_ID: ${{ secrets.FIREBASE_PROJECT_ID }}
FIREBASE_PRIVATE_KEY: ${{ secrets.FIREBASE_PRIVATE_KEY }}
FIREBASE_CLIENT_EMAIL: ${{ secrets.FIREBASE_CLIENT_EMAIL }}
FIREBASE_STORAGE_BUCKET: ${{ secrets.FIREBASE_STORAGE_BUCKET }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
run: docker compose -f docker-compose-test.yml run --rm test-${{ matrix.service }}
17 changes: 12 additions & 5 deletions .husky/pre-commit
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 ../..
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ docker-compose down

- User Service: http://localhost:3001
- Question Service: http://localhost:3000
- Matching Service: http://localhost:3002
- Frontend: http://localhost:5173
16 changes: 0 additions & 16 deletions backend/.env.sample

This file was deleted.

16 changes: 5 additions & 11 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

> Before proceeding to each microservice for more instructions:
1. Set-up either a local or cloud MongoDB.
1. Set up cloud MongoDB if not using docker. We recommend this if you are just testing out each microservice separately to avoid needing to manually set up multiple instances of local MongoDB. Else, if you are using docker-compose.yml to run PeerPrep, check out the READMEs in the different backend microservices to set up the env for the local MongoDB instances.

2. Set-up Firebase.
2. Set up Firebase.

## Setting-up local MongoDB (only if you are using Docker)

1. In the `backend` directory, create a copy of the `.env.sample` file and name it `.env`.

2. To set up credentials for the MongoDB database, update `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD` of the `.env` file.

3. Your local Mongo URI will be `mongodb://<MONGO_INITDB_ROOT_USERNAME>:<MONGO_INITDB_ROOT_PASSWORD>@mongo:27017/`. Take note of it as we will be using in the `.env` files in the various microservices later on.

4. You can view the MongoDB collections locally using Mongo Express. To set up Mongo Express, update `ME_CONFIG_BASICAUTH_USERNAME` and `ME_CONFIG_BASICAUTH_PASSWORD`. The username and password will be the login credentials when you access Mongo Express at http://localhost:8081.
3. Follow the instructions [here](https://nodejs.org/en/download/package-manager) to set up Node v20.

## Setting-up cloud MongoDB (in production)

Expand Down Expand Up @@ -95,6 +87,8 @@

## Setting-up Firebase

> For ease of testing, you can set up just one firebase to use across all the microservices that need it.
1. Go to https://console.firebase.google.com/u/0/.

2. Create a project and choose a project name. Navigate to `Storage` and click on it to activate it.
Expand Down
5 changes: 5 additions & 0 deletions backend/matching-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage
node_modules
tests
.env*
*.md
14 changes: 14 additions & 0 deletions backend/matching-service/.env.sample
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)
13 changes: 13 additions & 0 deletions backend/matching-service/Dockerfile
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"]
27 changes: 27 additions & 0 deletions backend/matching-service/README.md
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.
30 changes: 30 additions & 0 deletions backend/matching-service/app.ts
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;
70 changes: 70 additions & 0 deletions backend/matching-service/config/rabbitmq.ts
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)!;
};
22 changes: 22 additions & 0 deletions backend/matching-service/eslint.config.js
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,
];
Loading

0 comments on commit 4d178d4

Please sign in to comment.