Skip to content

Commit b03f149

Browse files
authored
Merge pull request #193 from CS3219-AY2425S1/enhancement/kubernetes
Add kubernetes
2 parents 8e85ae9 + 43dba5f commit b03f149

39 files changed

+539
-87
lines changed

.env.sample

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
## Common variables
22
COMPOSE_PATH_SEPARATOR=:
33
# Replace string between '...compose.' and '.yml' with 'dev' or 'prod'
4-
COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml
4+
COMPOSE_FILE=docker-compose.yml:docker-compose.prod.yml
55

66
## Frontend variables
7-
FRONTEND_PORT=3000
7+
FRONTEND_SERVICE_PORT=3000
88
# BASE_URI only needs to be provided if hosting outside of cluster
99
BASE_URI=
1010

1111
## Question service variables
12-
QUESTION_SVC_PORT=8000
12+
QUESTION_SERVICE_SERVICE_PORT=8000
1313
QUESTION_SVC_DB_URI=
1414

1515
## User service variables
16-
USER_SVC_PORT=3001
16+
USER_SERVICE_SERVICE_PORT=3001
1717
USER_SVC_DB_URI=
1818
JWT_SECRET=
1919
EMAIL_ADDRESS=
2020
EMAIL_PASSWORD=
2121

2222
## Matching service variables
23-
MATCHING_SVC_PORT=6969
23+
MATCHING_SERVICE_SERVICE_PORT=6969
2424

2525
## Collab service variables
26-
COLLAB_SVC_PORT=3002
26+
COLLAB_SERVICE_SERVICE_PORT=3002
2727
COLLAB_SVC_DB_URI=
2828
OPENAI_API_KEY=
2929

3030
## Redis variables
31-
REDIS_PORT=6379
31+
REDIS_SERVICE_PORT=6379
3232

3333
## Redisinsight variables
3434
REDIS_INSIGHT_PORT=5540
3535

3636
## API Gateway variables
37-
API_GATEWAY_PORT=
37+
API_GATEWAY_SERVICE_PORT=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.env
2+
kubernetes/secrets.yaml

api-gateway/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM nginx:1.26
2+
COPY nginx.conf /etc/nginx/nginx.conf
3+
COPY templates /etc/nginx/templates

api-gateway/nginx.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ http {
2727

2828
#gzip on;
2929

30+
add_header 'Access-Control-Allow-Origin' '*' always;
31+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
32+
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;
33+
3034
map $http_upgrade $connection_upgrade {
3135
default upgrade;
3236
'' close;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
upstream user-service {
2-
server user-service:$USER_SVC_PORT;
2+
server $USER_SERVICE_SERVICE_HOST:$USER_SERVICE_SERVICE_PORT;
33
}
44

55
upstream question-service {
6-
server question-service:$QUESTION_SVC_PORT;
6+
server $QUESTION_SERVICE_SERVICE_HOST:$QUESTION_SERVICE_SERVICE_PORT;
77
}
88

99
upstream matching-service {
10-
server matching-service:$MATCHING_SVC_PORT;
10+
server $MATCHING_SERVICE_SERVICE_HOST:$MATCHING_SERVICE_SERVICE_PORT;
1111
}
1212

1313
upstream collab-service {
14-
server collab-service:$COLLAB_SVC_PORT;
14+
server $COLLAB_SERVICE_SERVICE_HOST:$COLLAB_SERVICE_SERVICE_PORT;
1515
}
1616

1717
upstream frontend {
18-
server frontend:$FRONTEND_PORT;
18+
server $FRONTEND_SERVICE_HOST:$FRONTEND_SERVICE_PORT;
1919
}

api-gateway/templates/default.conf.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
include conf.d/api_conf.d/api_backends.conf;
22

33
server {
4-
listen $PORT;
5-
listen [::]:$PORT;
4+
listen $API_GATEWAY_SERVICE_PORT;
5+
listen [::]:$API_GATEWAY_SERVICE_PORT;
66

77
location /public/ {
88
include conf.d/api_conf.d/public_conf.d/*.conf;

collab-service/app/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ywsUtils from "y-websocket/bin/utils";
77
import { WebSocketServer } from "ws";
88
const setupWSConnection = ywsUtils.setupWSConnection;
99

10-
const PORT = process.env.PORT || 3002;
10+
const PORT = process.env.COLLAB_SERVICE_SERVICE_PORT || 3002;
1111
const server = http.createServer(index);
1212
const docs = ywsUtils.docs;
1313

docker-compose.yml

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
services:
22
api-gateway:
3-
image: nginx:1.26
4-
volumes:
5-
- ./api-gateway/templates:/etc/nginx/templates
6-
- ./api-gateway/nginx.conf:/etc/nginx/nginx.conf
3+
build:
4+
context: ./api-gateway
75
ports:
8-
- $API_GATEWAY_PORT:$API_GATEWAY_PORT
6+
- $API_GATEWAY_SERVICE_PORT:$API_GATEWAY_SERVICE_PORT
97
environment:
10-
- PORT=$API_GATEWAY_PORT
11-
- FRONTEND_PORT=$FRONTEND_PORT
12-
- USER_SVC_PORT=$USER_SVC_PORT
13-
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
14-
- MATCHING_SVC_PORT=$MATCHING_SVC_PORT
15-
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
8+
- API_GATEWAY_SERVICE_PORT=$API_GATEWAY_SERVICE_PORT
9+
- FRONTEND_SERVICE_PORT=$FRONTEND_SERVICE_PORT
10+
- FRONTEND_SERVICE_HOST=frontend
11+
- USER_SERVICE_SERVICE_PORT=$USER_SERVICE_SERVICE_PORT
12+
- USER_SERVICE_SERVICE_HOST=user-service
13+
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
14+
- QUESTION_SERVICE_SERVICE_HOST=question-service
15+
- MATCHING_SERVICE_SERVICE_PORT=$MATCHING_SERVICE_SERVICE_PORT
16+
- MATCHING_SERVICE_SERVICE_HOST=matching-service
17+
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
18+
- COLLAB_SERVICE_SERVICE_HOST=collab-service
1619
depends_on:
1720
- frontend
1821
- user-service
@@ -25,62 +28,62 @@ services:
2528
context: ./frontend
2629
args:
2730
- BASE_URI=$BASE_URI
28-
- API_GATEWAY_PORT=$API_GATEWAY_PORT
29-
environment:
30-
- PORT=$FRONTEND_PORT
31+
- API_GATEWAY_SERVICE_PORT=$API_GATEWAY_SERVICE_PORT
32+
- FRONTEND_SERVICE_PORT=$FRONTEND_SERVICE_PORT
3133
expose:
32-
- $FRONTEND_PORT
34+
- $FRONTEND_SERVICE_PORT
3335

3436
question-service:
3537
build:
3638
context: ./question-service
3739
environment:
38-
- PORT=$QUESTION_SVC_PORT
40+
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
3941
- DB_URI=$QUESTION_SVC_DB_URI
40-
- FRONTEND_PORT=$FRONTEND_PORT
4142
expose:
42-
- $QUESTION_SVC_PORT
43+
- $QUESTION_SERVICE_SERVICE_PORT
4344

4445
user-service:
4546
build:
4647
context: ./user-service
4748
environment:
48-
- PORT=$USER_SVC_PORT
49+
- USER_SERVICE_SERVICE_PORT=$USER_SERVICE_SERVICE_PORT
4950
- DB_URI=$USER_SVC_DB_URI
5051
- JWT_SECRET=$JWT_SECRET
5152
- EMAIL_ADDRESS=$EMAIL_ADDRESS
5253
- EMAIL_PASSWORD=$EMAIL_PASSWORD
5354
expose:
54-
- $USER_SVC_PORT
55+
- $USER_SERVICE_SERVICE_PORT
5556

5657
matching-service:
5758
build:
5859
context: ./matching-service
5960
environment:
60-
- PORT=$MATCHING_SVC_PORT
61-
- REDIS_HOST=redis
62-
- REDIS_PORT=$REDIS_PORT
63-
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
64-
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
61+
- MATCHING_SERVICE_SERVICE_PORT=$MATCHING_SERVICE_SERVICE_PORT
62+
- REDIS_SERVICE_HOST=redis
63+
- REDIS_SERVICE_PORT=$REDIS_SERVICE_PORT
64+
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
65+
- QUESTION_SERVICE_SERVICE_HOST=question-service
66+
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
67+
- COLLAB_SERVICE_SERVICE_HOST=collab-service
6568
expose:
66-
- $MATCHING_SVC_PORT
69+
- $MATCHING_SERVICE_SERVICE_PORT
6770
depends_on:
6871
- redis
6972
- question-service
7073
- collab-service
7174

72-
redis:
73-
image: redis:7.4-alpine
74-
restart: always
75-
expose:
76-
- $REDIS_PORT
77-
7875
collab-service:
7976
build:
8077
context: ./collab-service
8178
environment:
82-
- PORT=$COLLAB_SVC_PORT
79+
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
8380
- DB_URI=$COLLAB_SVC_DB_URI
8481
- OPENAI_API_KEY=$OPENAI_API_KEY
8582
expose:
86-
- $COLLAB_SVC_PORT
83+
- $COLLAB_SERVICE_SERVICE_PORT
84+
85+
redis:
86+
image: redis:7.4-alpine
87+
restart: always
88+
expose:
89+
- $REDIS_SERVICE_PORT

frontend/Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Base stage
22
FROM node:20-alpine AS base
33
ARG BASE_URI \
4-
API_GATEWAY_PORT
4+
API_GATEWAY_SERVICE_PORT \
5+
FRONTEND_SERVICE_PORT
56
WORKDIR /app
67
COPY package.json .
78
COPY yarn.lock .
89
RUN yarn install --frozen-lockfile
910
ENV NEXT_PUBLIC_BASE_URI=$BASE_URI \
10-
NEXT_PUBLIC_API_GATEWAY_PORT=$API_GATEWAY_PORT
11+
NEXT_PUBLIC_API_GATEWAY_PORT=$API_GATEWAY_SERVICE_PORT \
12+
PORT=$FRONTEND_SERVICE_PORT
1113

1214
# Production build stage
1315
FROM base AS build

frontend/components/collab/chat.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useAuth } from "@/app/auth/auth-context";
1313
import LoadingScreen from "@/components/common/loading-screen";
1414
import { sendAiMessage } from "@/lib/api/openai/send-ai-message";
1515
import { getChatHistory } from "@/lib/api/collab-service/get-chat-history";
16+
import { v4 as uuidv4 } from "uuid";
1617
import {
1718
AuthType,
1819
baseApiGatewayUri,
@@ -118,7 +119,7 @@ export default function Chat({ roomId }: { roomId: string }) {
118119

119120
const newMessage = {
120121
...message,
121-
id: message.messageIndex?.toString() || crypto.randomUUID(),
122+
id: message.messageIndex?.toString() || uuidv4(),
122123
timestamp: new Date(message.timestamp),
123124
};
124125

@@ -152,6 +153,15 @@ export default function Chat({ roomId }: { roomId: string }) {
152153
const sendMessage = async () => {
153154
if (!newMessage.trim() || !socket || !isConnected || !own_user_id) return;
154155

156+
if (!auth || !auth.token) {
157+
toast({
158+
title: "Access denied",
159+
description: "No authentication token found",
160+
variant: "destructive",
161+
});
162+
return;
163+
}
164+
155165
if (chatTarget === "partner") {
156166
socket.emit("sendMessage", {
157167
roomId,
@@ -160,16 +170,16 @@ export default function Chat({ roomId }: { roomId: string }) {
160170
});
161171
} else {
162172
const message: Message = {
163-
id: crypto.randomUUID(),
173+
id: uuidv4(),
164174
userId: own_user_id,
165175
text: newMessage,
166176
timestamp: new Date(),
167177
};
168178
setAiMessages((prev) => [...prev, message]);
169-
const response = await sendAiMessage(newMessage);
179+
const response = await sendAiMessage(auth?.token, newMessage);
170180
const data = await response.json();
171181
const aiMessage = {
172-
id: crypto.randomUUID(),
182+
id: uuidv4(),
173183
userId: "ai",
174184
text:
175185
data.data.choices && data.data.choices[0]?.message?.content

0 commit comments

Comments
 (0)