Skip to content

Commit

Permalink
Merge pull request #54 from CS3219-AY2425S1/matching-service-xy
Browse files Browse the repository at this point in the history
Add matching service
  • Loading branch information
xuelinglow authored Oct 20, 2024
2 parents 743855a + 4a6e591 commit ac25b05
Show file tree
Hide file tree
Showing 28 changed files with 4,571 additions and 30 deletions.
2 changes: 2 additions & 0 deletions backend/matching-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
20 changes: 20 additions & 0 deletions backend/matching-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official Node.js runtime as a parent image
FROM node:20-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install any dependencies
RUN npm install

# Bundle app source inside the Docker image
COPY . .

# Make port 5002 available to the world outside this container
EXPOSE 5002

# Define the command to run your app
CMD ["npm", "start"]
24 changes: 24 additions & 0 deletions backend/matching-service/config/firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Author(s): Andrew, Xiu Jia
require("dotenv").config();
const admin = require("firebase-admin");

const firebaseConfig = {
type: "service_account",
project_id: "cs3219-d4ee1",
private_key_id: "d9899e20a23a397cdce523437f42d50696aede61",
private_key: process.env.PRIVATE_KEY,
client_email: "[email protected]",
client_id: "106098472779235960036",
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_x509_cert_url: "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-hba0d%40cs3219-d4ee1.iam.gserviceaccount.com",
universe_domain: "googleapis.com"
};

admin.initializeApp({
credential: admin.credential.cert(firebaseConfig)
});
const db = admin.firestore();

module.exports = db;
26 changes: 26 additions & 0 deletions backend/matching-service/config/rabbitMQ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Established connection to the RabbitMQ
require("dotenv").config();
const amqp = require('amqplib');

const rabbitSettings = {
protocol: 'amqp',
hostname: `rabbitmq` || process.env.HOSTNAME || 'localhost',
port: 5672,
username: 'guest',
password: process.env.RABBIT_PASSWORD,
vhost: '/',
authMechanism: ['PLAIN', 'AMQPLAIN', 'EXTERNAL']
};

const connectToRabbitMQ = async () => {
try {
const conn = await amqp.connect(rabbitSettings);
const channel = await conn.createChannel();

return { conn, channel };
} catch (error) {
console.error('Error connecting to RabbitMQ:', error);
}
};

module.exports = connectToRabbitMQ;
41 changes: 41 additions & 0 deletions backend/matching-service/controller/matchController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Author(s): Calista, Xiu Jia, Xue Ling
const db = require("../config/firebase");
const matchCollection = db.collection("matches");

/**
* POST /add
*
* Creates match and stores in firebase
*
* Responses:
* - 500: Server error if something goes wrong while fetching data.
*/
const createMatch = async (user1, user2) => {
try {
const matchJson = {
user1: {
email: user1.email,
category: user1.topic,
complexity: user1.difficultyLevel,
isAny: user1.isAny
},
user2: {
email: user2.email,
category: user2.topic,
complexity: user2.difficultyLevel,
isAny: user2.isAny
},
datetime: new Date().toLocaleString("en-SG")
};

const response = await matchCollection.doc().set(matchJson);

return { status: 200, msg: "Match created successfully", response };
} catch (error) {
return { status: 500, error: error.message };
}
}

module.exports = {
createMatch
};
Loading

0 comments on commit ac25b05

Please sign in to comment.