Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
7b5fc4a
GitHub Classroom Feedback
github-classroom[bot] Sep 14, 2024
22bc64a
Setting up GitHub Classroom Feedback
github-classroom[bot] Sep 14, 2024
4a9ef3a
add deadline
github-classroom[bot] Sep 14, 2024
2e7f90e
Add user service
Sep 17, 2024
29a7d85
add .env file
chuababyy Sep 20, 2024
013d95b
Add gitignore
chuababyy Sep 22, 2024
b8cd78e
Add proficiency and display name field to user profile
chuababyy Sep 22, 2024
6d6f859
Move gitignore file
chuababyy Sep 22, 2024
fc2236f
Make skeleton for matching service
alex-setyawan Sep 25, 2024
5d15223
Question service (#4)
seewhyjay Sep 27, 2024
c0c0baf
Implement POST and DELETE api calls in frontend
yihfei Sep 27, 2024
8f6d083
Implement add question functionality
yihfei Sep 28, 2024
ee3adc5
Fix horizontal scrolling issue
yihfei Sep 28, 2024
7bd8f01
Finish CRUD basic functionality
yihfei Sep 28, 2024
4d059b8
Style modal form
yihfei Sep 28, 2024
987d4ac
Finish form styling
yihfei Sep 28, 2024
57cb33b
Fix JSON parsing
yihfei Sep 28, 2024
46e9c89
Feat: Dockerise Frontend
seewhyjay Sep 28, 2024
6342dd4
Merge pull request #11 from CS3219-AY2425S1/frontend-crud
seewhyjay Sep 28, 2024
6d7c32e
Improve validation and error handling
yihfei Sep 29, 2024
5613b95
Merge pull request #13 from CS3219-AY2425S1/frontend-crud
seewhyjay Sep 29, 2024
db2add0
Merge branch 'main' into add-user-service
chuababyy Oct 4, 2024
4fedf7e
Refactor frontend package
chuababyy Oct 4, 2024
fa4f0c8
Refactor frontend package
chuababyy Oct 4, 2024
0715ac2
Add login and logout function
chuababyy Oct 5, 2024
9058415
Add RegisterPage without logic
yihfei Oct 5, 2024
c689b05
Stop tracking .env file
yihfei Oct 5, 2024
5299f0f
AImplement registration logic
yihfei Oct 5, 2024
d860521
Add docker configuration for user service
lambraydon Oct 5, 2024
735003d
Add mongo-user service for local development
lambraydon Oct 6, 2024
f3a887e
Edit Navbar to remove non functional buttons
lambraydon Oct 6, 2024
f6bd3ef
Delete redundant node modules folder
lambraydon Oct 6, 2024
f21565c
Edit docker compose
lambraydon Oct 6, 2024
82cabf6
Edit docker compose to remove cloud URI
lambraydon Oct 6, 2024
5f51f18
Merge pull request #14 from CS3219-AY2425S1/user-service-docker
seewhyjay Oct 6, 2024
68e2fe4
Chore: Add .dockerignore to ignore node_modules (#15)
seewhyjay Oct 6, 2024
57a74ce
Add password legnth verification
yihfei Oct 6, 2024
dc235d3
Merge pull request #22 from CS3219-AY2425S1/reg-fix
lambraydon Oct 6, 2024
62b2487
Merge branch 'main' into matching-service
alex-setyawan Oct 12, 2024
61d33b6
Remove put method
alex-setyawan Oct 13, 2024
22f9a1c
remove /matcher route
alex-setyawan Oct 13, 2024
f6083d0
Delete redundant files
lambraydon Oct 15, 2024
402f464
Fix cookie during login
chuababyy Oct 15, 2024
5af7954
UI enhancements to login and register page
chuababyy Oct 15, 2024
54bddef
UI enhancements to Navbar
chuababyy Oct 15, 2024
9329035
Edit gitignore
chuababyy Oct 15, 2024
b6bd284
Set cookies in question service
chuababyy Oct 15, 2024
32152d3
Remove changes to question hook
chuababyy Oct 15, 2024
acc9665
Remove matching service
chuababyy Oct 15, 2024
d135847
Feat: Verify user token before allowing queries in Question Service
seewhyjay Oct 15, 2024
1edc316
Chore: Add security to OpenAPI specs
seewhyjay Oct 15, 2024
d5cca92
Merge pull request #24 from CS3219-AY2425S1/user-login
seewhyjay Oct 15, 2024
e1dcdaa
Merge branch 'main' into authenticate-questionservice
seewhyjay Oct 15, 2024
d1c2815
Chore: Update main project README
seewhyjay Oct 15, 2024
103660e
Chore: Delete package files
seewhyjay Oct 15, 2024
0a29f8b
Edit docker-compose image names
lambraydon Oct 16, 2024
6d36cac
Add kuburnetes manifests
lambraydon Oct 16, 2024
00ea0e8
Add cloudbuild.yaml
lambraydon Oct 17, 2024
e6f7e74
Edit hardcoded IP references
lambraydon Oct 18, 2024
b501dc7
Merge branch 'main' into kubernetes-config
lambraydon Oct 18, 2024
35f6ea9
Add CI/CD pipeline
lambraydon Oct 19, 2024
923abfa
Update ci.yml
lambraydon Oct 19, 2024
90d83b8
Edit ci.yml: Include tests
lambraydon Oct 19, 2024
8dce36d
Edit ci.yml gcloud build context
lambraydon Oct 19, 2024
97b8c3e
Edit deploy.yaml to run after CI
lambraydon Oct 19, 2024
a18b05c
Edit deploy.yml
lambraydon Oct 19, 2024
06d9194
Edit deploy.yml to include CD
lambraydon Oct 19, 2024
2831c85
Add .sh file in gitignore
lambraydon Oct 19, 2024
f8998ab
Ignore k8s_deploy.sh
lambraydon Oct 19, 2024
1028e51
Debug deploy.yml
lambraydon Oct 19, 2024
d3b6311
Add auth-plugin for deploy.yml
lambraydon Oct 19, 2024
b2d14c8
Add force restart deployment step
lambraydon Oct 19, 2024
f5fbad2
Edit deploy.yml: Remove tests
lambraydon Oct 19, 2024
8a6741a
Add mongo-seed-job.yaml
lambraydon Oct 19, 2024
395306d
Merge pull request #25 from CS3219-AY2425S1/authenticate-questionservice
seewhyjay Oct 20, 2024
c6c9940
Matching service docker (#30)
lambraydon Oct 20, 2024
5d6222c
Matching service refactor (#31)
lambraydon Oct 25, 2024
a736cab
Fix Matching Service Frontend Issues
yihfei Oct 25, 2024
a24fc99
Remove commented out code
yihfei Oct 25, 2024
12b27e4
Merge pull request #37 from CS3219-AY2425S1/frontend-matching-fix
lambraydon Oct 25, 2024
555d0f8
Feat: Tests for UserService
seewhyjay Oct 26, 2024
cdf1494
Fix: Use correct docker compose file
seewhyjay Oct 26, 2024
4fa34a0
Fix: Use pytest
seewhyjay Oct 26, 2024
4bebb75
Feat: Add new tests
seewhyjay Oct 26, 2024
f27e690
Fix: Made question service errors more detailed
seewhyjay Oct 28, 2024
097fe95
Fix: Frontend verify user token before correctly
seewhyjay Oct 28, 2024
59fec20
Merge pull request #42 from CS3219-AY2425S1/integration-test
lambraydon Oct 29, 2024
5d65ef6
Fix: User token check
seewhyjay Oct 29, 2024
bf3343d
Fix: Seed questions with Question Title as name instead of Question I…
seewhyjay Oct 30, 2024
f55dc84
Matching service refactor (#43)
lambraydon Nov 1, 2024
3c6d23c
Add language and syntax highlighting in code editor
chuababyy Nov 2, 2024
00f7018
Merge branch 'main' into integration-test
seewhyjay Nov 2, 2024
88bac5f
Feat: Authenticate MatchingService requests
seewhyjay Nov 2, 2024
ad96319
Fix: Enable cors for frontend URL on MatchingService
seewhyjay Nov 2, 2024
7a8da7c
Chore: Add more tests for question service
seewhyjay Nov 2, 2024
d2c4bc1
Add local storage for different languages
chuababyy Nov 2, 2024
bb5ae71
Change saving to every 2s
chuababyy Nov 2, 2024
fddbef3
Merge pull request #45 from CS3219-AY2425S1/syntax-highlight
seewhyjay Nov 3, 2024
141bfa0
Merge pull request #44 from CS3219-AY2425S1/integration-test
chuababyy Nov 3, 2024
df892a3
Add history feature
chuababyy Nov 3, 2024
d035873
Merge branch 'main' into history
chuababyy Nov 3, 2024
090ff6b
Feat: Disallow users from sending match request if they're already in…
seewhyjay Nov 4, 2024
29244c3
Feat: Tests for matching service
seewhyjay Nov 4, 2024
b556ef1
Fix: Force maximum number of workers
seewhyjay Nov 4, 2024
05e8c35
Add history feature
chuababyy Nov 4, 2024
e17608c
Fix: Run tests sequentially
seewhyjay Nov 4, 2024
1e802dd
Chore: Change debug level
seewhyjay Nov 4, 2024
3ba47ad
Fix: Use a counter to ensure different usernames
seewhyjay Nov 4, 2024
96aad95
Fix: Still can't figure it out
seewhyjay Nov 4, 2024
cf2fa72
Fix: Remove the test
seewhyjay Nov 4, 2024
8b02a99
Fix: Always cancel requests no matter what
seewhyjay Nov 4, 2024
6c55e18
Add history feature
chuababyy Nov 4, 2024
164de44
Add question description
chuababyy Nov 4, 2024
5e69537
Merge pull request #49 from CS3219-AY2425S1/history
lambraydon Nov 5, 2024
8dc3e41
Implement transcient chat service
yihfei Nov 6, 2024
84f3c1f
Add timestamp and username in chatlogs
yihfei Nov 6, 2024
9335003
Add styling
yihfei Nov 6, 2024
a0d88ce
Remove redundant files
yihfei Nov 6, 2024
457f53f
Add docker config for chat service
lambraydon Nov 6, 2024
86a2030
Delete redundant files
lambraydon Nov 6, 2024
9762749
Merge branch 'main' into kubernetes-config
lambraydon Nov 6, 2024
317da66
Edit useQuestionTable.jsx: Fix merge conflict
lambraydon Nov 6, 2024
5680360
Add kubernetes manifests for matching-service
lambraydon Nov 6, 2024
10de11d
Add kubernetes manifests for collaboration service
lambraydon Nov 6, 2024
6b945be
Edit hardcoded IP references in frontend
lambraydon Nov 6, 2024
5b738be
Edit hardcode IPs in matching service
lambraydon Nov 6, 2024
f04c716
Edit: Remove hardcoded IP in question, collab and matching services
lambraydon Nov 6, 2024
33fec30
Merge pull request #53 from CS3219-AY2425S1/ChatService
alex-setyawan Nov 6, 2024
634b241
Edit hardcoded IP
lambraydon Nov 6, 2024
00b79cc
Edit question service
lambraydon Nov 6, 2024
49c4463
Merge branch 'main' into kubernetes-config
lambraydon Nov 7, 2024
2557e99
Fix race condition on chat and collab service
yihfei Nov 7, 2024
636ce64
Fix race conditions for leave room alerts
yihfei Nov 7, 2024
cd470e3
Feat: Add nginx frontend as api router
seewhyjay Nov 7, 2024
df25913
Chore: Cleanup
seewhyjay Nov 7, 2024
6cbe90d
Chore: more cleanup
seewhyjay Nov 7, 2024
7191e75
Feat: Create new server blocks in nginx to route different socket.io …
seewhyjay Nov 7, 2024
daf77e7
Fix: Listen to the correct ports for nginx
seewhyjay Nov 7, 2024
6f39212
Chore: Update integration tests to use the reverse proxy
seewhyjay Nov 7, 2024
5c114ac
Merge branch 'collab-fix' into kubernetes-config
lambraydon Nov 7, 2024
f012d0d
Chore: Update docker-compose.yaml
seewhyjay Nov 7, 2024
c683667
Merge api-routing
yihfei Nov 7, 2024
f25a970
Merge api-routing fix
yihfei Nov 7, 2024
7d1aa36
Merge branch 'main' into api-routing
seewhyjay Nov 7, 2024
c4c930a
Merge pull request #55 from CS3219-AY2425S1/api-routing
seewhyjay Nov 7, 2024
6258a3b
Fix merge conflict
yihfei Nov 7, 2024
7ec277e
Fix: Add networks to nginx container (#58)
seewhyjay Nov 7, 2024
f82c840
Pull change from main
yihfei Nov 7, 2024
0c93978
Merge pull request #57 from CS3219-AY2425S1/collab-fix
lambraydon Nov 7, 2024
9e1dbe4
Fix matching page vertical overflow
yihfei Nov 7, 2024
9934921
Merge branch 'main' into kubernetes-config
lambraydon Nov 7, 2024
de6ad95
Enhance styling for matching page and navbar
yihfei Nov 7, 2024
89a26b9
Add cors config for chat-service
lambraydon Nov 7, 2024
03dea3b
Add cors config for collab-service
lambraydon Nov 7, 2024
79cfb37
Edit IP references
lambraydon Nov 7, 2024
0894d7c
Add manifests for chat service
lambraydon Nov 7, 2024
97a55c3
Edit manifests
lambraydon Nov 7, 2024
16ff60a
Fix overflow editor issue
yihfei Nov 7, 2024
86418f0
Edit conditional CORS setting depending on prod or dev env
lambraydon Nov 7, 2024
7a7a7c5
Add chat, matching and collab build and deploy workflow
lambraydon Nov 7, 2024
eb3b3aa
Add import_question_prod.py for seeding on prod env
lambraydon Nov 7, 2024
f6b23c1
Add Dockerfile
lambraydon Nov 7, 2024
e580e5d
Merge pull request #59 from CS3219-AY2425S1/styling
lambraydon Nov 7, 2024
a6c7b29
Add code execution
alex-setyawan Nov 7, 2024
5babda4
Merge branch 'main' into execution-service
alex-setyawan Nov 7, 2024
4b2cf6a
Merge branch 'main' into kubernetes-config
lambraydon Nov 8, 2024
1047163
Add workflow_dispatch trigger for CI/CD workflow
lambraydon Nov 8, 2024
efa7e32
Edit deploy workflow trigger
lambraydon Nov 8, 2024
8245620
Add conditional CORS policy for user service in prod
lambraydon Nov 8, 2024
9593860
Add conditional CORS policy for Matching in prod
lambraydon Nov 8, 2024
3e1f10e
Minor edit
alex-setyawan Nov 8, 2024
355cc3c
Add edit button
alex-setyawan Nov 8, 2024
74dcc00
Add condifional verification
lambraydon Nov 8, 2024
776d2e3
Add workflow_dispatch trigger for build deploy workflow
lambraydon Nov 8, 2024
549c658
Feat: Add chatservice to nginx.conf
seewhyjay Nov 8, 2024
124c6fe
Chore: Change website title
seewhyjay Nov 8, 2024
c256256
Fix: Add chat service to tests docker compose file so that nginx can …
seewhyjay Nov 8, 2024
1a04794
Merge pull request #61 from CS3219-AY2425S1/api-routing
seewhyjay Nov 8, 2024
8d5f265
Add secrets
lambraydon Nov 8, 2024
f575c1a
Update deploy.yml to pull secrets
lambraydon Nov 8, 2024
280f257
Merge branch 'main' into kubernetes-config
seewhyjay Nov 9, 2024
9ab4976
Merge pull request #28 from CS3219-AY2425S1/kubernetes-config
seewhyjay Nov 9, 2024
9963e58
Remove redundant backend
alex-setyawan Nov 9, 2024
a00bf57
Fix endpoint (#62)
lambraydon Nov 9, 2024
786cab2
Merge pull request #60 from CS3219-AY2425S1/execution-service
lambraydon Nov 9, 2024
8947771
Authenticate using JWT secret at each service instead of calling user…
seewhyjay Nov 10, 2024
ecbf149
Add edit profile feature
chuababyy Nov 10, 2024
0b54441
Fix: Allow PATCH for Preflight requests in nginx
seewhyjay Nov 10, 2024
b7a2206
Fix: Allow PATCH not PUT
seewhyjay Nov 10, 2024
b2d145a
Fix edit profile
chuababyy Nov 10, 2024
95524d1
Merge pull request #64 from CS3219-AY2425S1/edit-user
seewhyjay Nov 10, 2024
64a4448
Chore: Update readme
seewhyjay Nov 10, 2024
a2e680f
Merge pull request #65 from CS3219-AY2425S1/update-readme
alex-setyawan Nov 10, 2024
d19649f
Collab refactor (#67)
lambraydon Nov 10, 2024
670ec8c
Add button disabling until output returned (#69)
alex-setyawan Nov 13, 2024
20548be
Collab editor fix (#70)
lambraydon Nov 13, 2024
eb312ab
Add redis to chat
yihfei Nov 13, 2024
ad4b73b
Chore: Update docker-compose for tests and backend
seewhyjay Nov 13, 2024
1707cdc
Add styling
yihfei Nov 13, 2024
53f78d5
Pull changes
yihfei Nov 13, 2024
d0d3b5f
Chore: Update readme
seewhyjay Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .github/.keep
Empty file.
127 changes: 127 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: CI/CD - Build and Deploy

on:
push:
branches:
- kubernetes-config
workflow_dispatch:

jobs:
ci:
name: Build and Push Docker Images

runs-on: ubuntu-latest

timeout-minutes: 10

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- name: Configure Docker for GCR
run: gcloud auth configure-docker

- name: Submit Cloud Build
run: gcloud builds submit --config=cloudbuild.yaml .

cd:
name: Deploy to GKE

runs-on: ubuntu-latest

needs: ci

timeout-minutes: 10

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.sha }}

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}

- name: Install gke-gcloud-auth-plugin
run: gcloud components install gke-gcloud-auth-plugin

- name: Get GKE Credentials
run: gcloud container clusters get-credentials ${{ secrets.GKE_CLUSTER_NAME }} --zone ${{ secrets.GKE_CLUSTER_ZONE }}

- name: Set kubectl context
run: kubectl config set-context $(kubectl config current-context) --namespace=default

- name: Create Mongo-User Secret in Kubernetes
env:
MONGO_ROOT_USERNAME: ${{ secrets.MONGO_ROOT_USERNAME }}
MONGO_ROOT_PASSWORD: ${{ secrets.MONGO_ROOT_PASSWORD }}
run: |
kubectl create secret generic mongo-user-secret \
--from-literal=MONGO_INITDB_ROOT_USERNAME=$MONGO_ROOT_USERNAME \
--from-literal=MONGO_INITDB_ROOT_PASSWORD=$MONGO_ROOT_PASSWORD \
--namespace default --dry-run=client -o yaml | kubectl apply -f -

- name: Create Mongo-Question Secret in Kubernetes
env:
MONGO_ROOT_USERNAME: ${{ secrets.MONGO_ROOT_USERNAME }}
MONGO_ROOT_PASSWORD: ${{ secrets.MONGO_ROOT_PASSWORD }}
run: |
kubectl create secret generic mongo-question-secret \
--from-literal=MONGO_INITDB_ROOT_USERNAME=$MONGO_ROOT_USERNAME \
--from-literal=MONGO_INITDB_ROOT_PASSWORD=$MONGO_ROOT_PASSWORD \
--namespace default --dry-run=client -o yaml | kubectl apply -f -

- name: Create Secrets for question-service and user-service in Kubernetes
env:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
DB_LOCAL_URI: ${{ secrets.DB_LOCAL_URI }}
MONGO_URI: ${{ secrets.MONGO_URI }}
run: |
kubectl create secret generic user-service-secret \
--from-literal=JWT_SECRET=$JWT_SECRET \
--from-literal=DB_LOCAL_URI=$DB_LOCAL_URI \
--namespace default --dry-run=client -o yaml | kubectl apply -f -

kubectl create secret generic question-service-secret \
--from-literal=MONGO_URI=$MONGO_URI \
--namespace default --dry-run=client -o yaml | kubectl apply -f -

- name: Deploy to GKE
run: |
kubectl apply -f k8s/mongo-user-db/
kubectl apply -f k8s/mongo-question-db/
kubectl apply -f k8s/user-service/
kubectl apply -f k8s/question-service/
kubectl apply -f k8s/frontend/
kubectl apply -f k8s/matching-service/
kubectl apply -f k8s/collaboration-service/
kubectl apply -f k8s/chat-service/

- name: Force Restart Deployment
run: |
kubectl rollout restart deployment user-service
kubectl rollout restart deployment mongo-question
kubectl rollout restart deployment frontend
kubectl rollout restart deployment matching-service
kubectl rollout restart deployment collaboration-service
kubectl rollout restart deployment chat-service
kubectl rollout restart deployment question-service


20 changes: 20 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Integration Test
on: [push]
jobs:
integration-tests:
name: integration-tests
runs-on: ubuntu-latest
steps:
# Check out the repository
- name: Checkout code
uses: actions/checkout@v4

# Run Docker Compose Action
- name: Run integration tests
uses: adambirds/[email protected]
with:
compose-file: "./docker-compose-tests.yaml"
up-flags: "--build"
down-flags: "--volumes"
test-container: "tests"
test-command: "pytest --log-cli-level=DEBUG"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.vscode
node_modules
k8s/k8s_deploy.sh
35 changes: 35 additions & 0 deletions ChatService/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import express from 'express';
import { Server } from 'socket.io';
import http from 'http';
import cors from 'cors';
import socket from './config/socket.js';

const app = express();
const port = 3003;
const server = http.createServer(app);
const frontendURL = process.env.FRONTEND_URL || "http://localhost:8080";

const io = process.env.FRONTEND_URL
? new Server(server, {
cors: {
origin: frontendURL,
methods: ['GET', 'POST'],
credentials: true
}
})
: new Server(server);

if (process.env.FRONTEND_URL) {
app.use(cors({
origin: frontendURL,
credentials: true
}));
}

app.use(express.json());
socket.createSocket(io);

server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});

21 changes: 21 additions & 0 deletions ChatService/config/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { createClient } from 'redis';

function createRedisConnection() {
try {
const client = createClient({
socket: {
host: 'redis-chat',
port: 6379
}
});
client.connect();
console.log("Successfully connected to Redis-Chat");
return client;
} catch (error) {
console.log("Error connecting to Redis-Chat ", error);
return null;
}
}

export default createRedisConnection;
58 changes: 58 additions & 0 deletions ChatService/config/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import roomService from '../services/roomService.js';
const { createRoom, getRoom, addMessage } = roomService;

async function createSocket(io) {
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
const userId = socket.handshake.query.userId;

socket.on('joinRoom', async ({ roomId }) => {
console.log(`User ${socket.id} attempting to join room: ${roomId}`);
socket.join(roomId);

// Get the number of clients currently in the room
const clients = io.sockets.adapter.rooms.get(roomId)?.size || 0;

// If there are now two clients, create the room and broadcast the content to all clients in the room
if (clients > 1) {
const room = await createRoom(roomId, userId, userId); // Assuming you pass the user IDs here

// Broadcast to everyone in the room including the joining client
io.to(roomId).emit('load_room_content', {
messages: room.messages,
});
} else {
const room = await getRoom(roomId); // Await for the room data to be retrieved
if (room) {
// If the room exists, broadcast the content to the joining client
socket.emit('load_room_content', {
messages: room.messages,
});
} else {
console.error(`Room ${roomId} does not exist.`);
}
}
});

socket.on('chat message', async ({ roomId, msg, username }) => {
console.log(`User ${socket.id} sending message in room: ${roomId}`);
const room = await getRoom(roomId); // Await for the room data to be retrieved

if (room) {
const newMessage = await addMessage(roomId, msg, username); // Ensure the message is added asynchronously
console.log(`Updated chat content in room ${roomId}. Broadcasting to other users.`);

// Emit the updated message to the room
socket.to(roomId).emit('chat message', { newMessage });
// Emit to the client who sent the message
socket.emit('chat message', { newMessage });
} else {
console.error(`Failed to update chat for room ${roomId} by user ${socket.id}. Room or content may be missing.`);
}
});
});
}

export default {
createSocket
};
9 changes: 9 additions & 0 deletions ChatService/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:22

WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app

EXPOSE 3003
ENTRYPOINT ["npm", "start"]
29 changes: 29 additions & 0 deletions ChatService/models/room-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Room {
constructor(roomId, user1Id, user2Id) {
this.roomId = roomId;
this.users = [user1Id, user2Id];
this.messages = []; // Initialize an empty array to store messages
}


addMessage(content, senderUsername) {
const message = {
content,
senderUsername,
timestamp: new Date().toISOString() // Add a timestamp in ISO format
};
this.messages.push(message); // Add the structured message to the messages array
return message;
}


getRoomState() {
return {
roomId: this.roomId,
users: this.users,
messages: this.messages, // Return the list of messages
};
}
}

export default Room;
Loading