Skip to content

Commit

Permalink
Merge pull request #193 from CS3219-AY2425S1/enhancement/kubernetes
Browse files Browse the repository at this point in the history
Add kubernetes
  • Loading branch information
jq1836 authored Nov 5, 2024
2 parents 8e85ae9 + 43dba5f commit b03f149
Show file tree
Hide file tree
Showing 39 changed files with 539 additions and 87 deletions.
16 changes: 8 additions & 8 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
## Common variables
COMPOSE_PATH_SEPARATOR=:
# Replace string between '...compose.' and '.yml' with 'dev' or 'prod'
COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml
COMPOSE_FILE=docker-compose.yml:docker-compose.prod.yml

## Frontend variables
FRONTEND_PORT=3000
FRONTEND_SERVICE_PORT=3000
# BASE_URI only needs to be provided if hosting outside of cluster
BASE_URI=

## Question service variables
QUESTION_SVC_PORT=8000
QUESTION_SERVICE_SERVICE_PORT=8000
QUESTION_SVC_DB_URI=

## User service variables
USER_SVC_PORT=3001
USER_SERVICE_SERVICE_PORT=3001
USER_SVC_DB_URI=
JWT_SECRET=
EMAIL_ADDRESS=
EMAIL_PASSWORD=

## Matching service variables
MATCHING_SVC_PORT=6969
MATCHING_SERVICE_SERVICE_PORT=6969

## Collab service variables
COLLAB_SVC_PORT=3002
COLLAB_SERVICE_SERVICE_PORT=3002
COLLAB_SVC_DB_URI=
OPENAI_API_KEY=

## Redis variables
REDIS_PORT=6379
REDIS_SERVICE_PORT=6379

## Redisinsight variables
REDIS_INSIGHT_PORT=5540

## API Gateway variables
API_GATEWAY_PORT=
API_GATEWAY_SERVICE_PORT=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
kubernetes/secrets.yaml
3 changes: 3 additions & 0 deletions api-gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM nginx:1.26
COPY nginx.conf /etc/nginx/nginx.conf
COPY templates /etc/nginx/templates
4 changes: 4 additions & 0 deletions api-gateway/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ http {

#gzip on;

add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
Expand Down
10 changes: 5 additions & 5 deletions api-gateway/templates/api_conf.d/api_backends.conf.template
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
upstream user-service {
server user-service:$USER_SVC_PORT;
server $USER_SERVICE_SERVICE_HOST:$USER_SERVICE_SERVICE_PORT;
}

upstream question-service {
server question-service:$QUESTION_SVC_PORT;
server $QUESTION_SERVICE_SERVICE_HOST:$QUESTION_SERVICE_SERVICE_PORT;
}

upstream matching-service {
server matching-service:$MATCHING_SVC_PORT;
server $MATCHING_SERVICE_SERVICE_HOST:$MATCHING_SERVICE_SERVICE_PORT;
}

upstream collab-service {
server collab-service:$COLLAB_SVC_PORT;
server $COLLAB_SERVICE_SERVICE_HOST:$COLLAB_SERVICE_SERVICE_PORT;
}

upstream frontend {
server frontend:$FRONTEND_PORT;
server $FRONTEND_SERVICE_HOST:$FRONTEND_SERVICE_PORT;
}
4 changes: 2 additions & 2 deletions api-gateway/templates/default.conf.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include conf.d/api_conf.d/api_backends.conf;

server {
listen $PORT;
listen [::]:$PORT;
listen $API_GATEWAY_SERVICE_PORT;
listen [::]:$API_GATEWAY_SERVICE_PORT;

location /public/ {
include conf.d/api_conf.d/public_conf.d/*.conf;
Expand Down
2 changes: 1 addition & 1 deletion collab-service/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ywsUtils from "y-websocket/bin/utils";
import { WebSocketServer } from "ws";
const setupWSConnection = ywsUtils.setupWSConnection;

const PORT = process.env.PORT || 3002;
const PORT = process.env.COLLAB_SERVICE_SERVICE_PORT || 3002;
const server = http.createServer(index);
const docs = ywsUtils.docs;

Expand Down
71 changes: 37 additions & 34 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
services:
api-gateway:
image: nginx:1.26
volumes:
- ./api-gateway/templates:/etc/nginx/templates
- ./api-gateway/nginx.conf:/etc/nginx/nginx.conf
build:
context: ./api-gateway
ports:
- $API_GATEWAY_PORT:$API_GATEWAY_PORT
- $API_GATEWAY_SERVICE_PORT:$API_GATEWAY_SERVICE_PORT
environment:
- PORT=$API_GATEWAY_PORT
- FRONTEND_PORT=$FRONTEND_PORT
- USER_SVC_PORT=$USER_SVC_PORT
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
- MATCHING_SVC_PORT=$MATCHING_SVC_PORT
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
- API_GATEWAY_SERVICE_PORT=$API_GATEWAY_SERVICE_PORT
- FRONTEND_SERVICE_PORT=$FRONTEND_SERVICE_PORT
- FRONTEND_SERVICE_HOST=frontend
- USER_SERVICE_SERVICE_PORT=$USER_SERVICE_SERVICE_PORT
- USER_SERVICE_SERVICE_HOST=user-service
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
- QUESTION_SERVICE_SERVICE_HOST=question-service
- MATCHING_SERVICE_SERVICE_PORT=$MATCHING_SERVICE_SERVICE_PORT
- MATCHING_SERVICE_SERVICE_HOST=matching-service
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
- COLLAB_SERVICE_SERVICE_HOST=collab-service
depends_on:
- frontend
- user-service
Expand All @@ -25,62 +28,62 @@ services:
context: ./frontend
args:
- BASE_URI=$BASE_URI
- API_GATEWAY_PORT=$API_GATEWAY_PORT
environment:
- PORT=$FRONTEND_PORT
- API_GATEWAY_SERVICE_PORT=$API_GATEWAY_SERVICE_PORT
- FRONTEND_SERVICE_PORT=$FRONTEND_SERVICE_PORT
expose:
- $FRONTEND_PORT
- $FRONTEND_SERVICE_PORT

question-service:
build:
context: ./question-service
environment:
- PORT=$QUESTION_SVC_PORT
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
- DB_URI=$QUESTION_SVC_DB_URI
- FRONTEND_PORT=$FRONTEND_PORT
expose:
- $QUESTION_SVC_PORT
- $QUESTION_SERVICE_SERVICE_PORT

user-service:
build:
context: ./user-service
environment:
- PORT=$USER_SVC_PORT
- USER_SERVICE_SERVICE_PORT=$USER_SERVICE_SERVICE_PORT
- DB_URI=$USER_SVC_DB_URI
- JWT_SECRET=$JWT_SECRET
- EMAIL_ADDRESS=$EMAIL_ADDRESS
- EMAIL_PASSWORD=$EMAIL_PASSWORD
expose:
- $USER_SVC_PORT
- $USER_SERVICE_SERVICE_PORT

matching-service:
build:
context: ./matching-service
environment:
- PORT=$MATCHING_SVC_PORT
- REDIS_HOST=redis
- REDIS_PORT=$REDIS_PORT
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
- MATCHING_SERVICE_SERVICE_PORT=$MATCHING_SERVICE_SERVICE_PORT
- REDIS_SERVICE_HOST=redis
- REDIS_SERVICE_PORT=$REDIS_SERVICE_PORT
- QUESTION_SERVICE_SERVICE_PORT=$QUESTION_SERVICE_SERVICE_PORT
- QUESTION_SERVICE_SERVICE_HOST=question-service
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
- COLLAB_SERVICE_SERVICE_HOST=collab-service
expose:
- $MATCHING_SVC_PORT
- $MATCHING_SERVICE_SERVICE_PORT
depends_on:
- redis
- question-service
- collab-service

redis:
image: redis:7.4-alpine
restart: always
expose:
- $REDIS_PORT

collab-service:
build:
context: ./collab-service
environment:
- PORT=$COLLAB_SVC_PORT
- COLLAB_SERVICE_SERVICE_PORT=$COLLAB_SERVICE_SERVICE_PORT
- DB_URI=$COLLAB_SVC_DB_URI
- OPENAI_API_KEY=$OPENAI_API_KEY
expose:
- $COLLAB_SVC_PORT
- $COLLAB_SERVICE_SERVICE_PORT

redis:
image: redis:7.4-alpine
restart: always
expose:
- $REDIS_SERVICE_PORT
6 changes: 4 additions & 2 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Base stage
FROM node:20-alpine AS base
ARG BASE_URI \
API_GATEWAY_PORT
API_GATEWAY_SERVICE_PORT \
FRONTEND_SERVICE_PORT
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn install --frozen-lockfile
ENV NEXT_PUBLIC_BASE_URI=$BASE_URI \
NEXT_PUBLIC_API_GATEWAY_PORT=$API_GATEWAY_PORT
NEXT_PUBLIC_API_GATEWAY_PORT=$API_GATEWAY_SERVICE_PORT \
PORT=$FRONTEND_SERVICE_PORT

# Production build stage
FROM base AS build
Expand Down
18 changes: 14 additions & 4 deletions frontend/components/collab/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAuth } from "@/app/auth/auth-context";
import LoadingScreen from "@/components/common/loading-screen";
import { sendAiMessage } from "@/lib/api/openai/send-ai-message";
import { getChatHistory } from "@/lib/api/collab-service/get-chat-history";
import { v4 as uuidv4 } from "uuid";
import {
AuthType,
baseApiGatewayUri,
Expand Down Expand Up @@ -118,7 +119,7 @@ export default function Chat({ roomId }: { roomId: string }) {

const newMessage = {
...message,
id: message.messageIndex?.toString() || crypto.randomUUID(),
id: message.messageIndex?.toString() || uuidv4(),
timestamp: new Date(message.timestamp),
};

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

if (!auth || !auth.token) {
toast({
title: "Access denied",
description: "No authentication token found",
variant: "destructive",
});
return;
}

if (chatTarget === "partner") {
socket.emit("sendMessage", {
roomId,
Expand All @@ -160,16 +170,16 @@ export default function Chat({ roomId }: { roomId: string }) {
});
} else {
const message: Message = {
id: crypto.randomUUID(),
id: uuidv4(),
userId: own_user_id,
text: newMessage,
timestamp: new Date(),
};
setAiMessages((prev) => [...prev, message]);
const response = await sendAiMessage(newMessage);
const response = await sendAiMessage(auth?.token, newMessage);
const data = await response.json();
const aiMessage = {
id: crypto.randomUUID(),
id: uuidv4(),
userId: "ai",
text:
data.data.choices && data.data.choices[0]?.message?.content
Expand Down
1 change: 0 additions & 1 deletion frontend/components/collab/question-display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default function QuestionDisplay({
useEffect(() => {
async function fetchQuestion() {
try {
console.log(auth);
if (!auth || !auth.token) {
toast({
title: "Access denied",
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/api/api-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const constructUri: (
authType: AuthType,
serviceName: string
) => string = (baseUri: string, authType: AuthType, serviceName: string) =>
`${baseApiGatewayUri(baseUri)}/${constructUriSuffix(authType, serviceName)}`;
`${baseApiGatewayUri(baseUri)}${constructUriSuffix(authType, serviceName)}`;

export const userServiceUri: (baseUri: string, authType: AuthType) => string = (
baseUri: string,
Expand Down
3 changes: 2 additions & 1 deletion frontend/lib/api/openai/send-ai-message.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AuthType, collabServiceUri } from "@/lib/api/api-uri";

export const sendAiMessage = async (message: string) => {
export const sendAiMessage = async (jwtToken: string, message: string) => {
const response = await fetch(
`${collabServiceUri(window.location.hostname, AuthType.Private)}/collab/send-ai-message`,
{
method: "POST",
headers: {
Authorization: `Bearer ${jwtToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ message: message }),
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"swr": "^2.2.5",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
"uuid": "^11.0.2",
"y-monaco": "^0.1.6",
"y-websocket": "^2.0.4",
"yarn": "^1.22.22",
Expand Down
Loading

0 comments on commit b03f149

Please sign in to comment.