Skip to content

Commit

Permalink
Merge pull request #167 from CS3219-AY2425S1/enhancement/api-gateway
Browse files Browse the repository at this point in the history
Add API gateway
  • Loading branch information
jq1836 authored Nov 5, 2024
2 parents 01f9344 + dda9217 commit 8e85ae9
Show file tree
Hide file tree
Showing 51 changed files with 425 additions and 133 deletions.
9 changes: 6 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ FRONTEND_PORT=3000
BASE_URI=

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

## User service variables
USER_SVC_PORT=
USER_SVC_PORT=3001
USER_SVC_DB_URI=
JWT_SECRET=
EMAIL_ADDRESS=
Expand All @@ -31,4 +31,7 @@ OPENAI_API_KEY=
REDIS_PORT=6379

## Redisinsight variables
REDIS_INSIGHT_PORT=5540
REDIS_INSIGHT_PORT=5540

## API Gateway variables
API_GATEWAY_PORT=
36 changes: 36 additions & 0 deletions api-gateway/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

include /etc/nginx/conf.d/*.conf;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
location /admin/matching-service/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
location /admin/question-service/ {
location /admin/question-service/questions {
proxy_pass http://question-service/questions;
}
}
19 changes: 19 additions & 0 deletions api-gateway/templates/api_conf.d/api_backends.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
upstream user-service {
server user-service:$USER_SVC_PORT;
}

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

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

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

upstream frontend {
server frontend:$FRONTEND_PORT;
}
23 changes: 23 additions & 0 deletions api-gateway/templates/api_conf.d/auth.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
location /verify-token {
internal;

proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_pass http://user-service/auth/verify-token;
}

location /verify-owner {
internal;

proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_pass http://user-service/auth/verify-owner;
}

location /verify-admin {
internal;

proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_pass http://user-service/auth/verify-admin;
}
3 changes: 3 additions & 0 deletions api-gateway/templates/api_conf.d/frontend.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
location / {
proxy_pass http://frontend;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
location /owner/matching-service/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
location /owner/question-service/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
location /private/collab-service/ {
proxy_pass http://collab-service/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
location /private/matching-service/ {
location /private/matching-service/match {
proxy_pass http://matching-service/match;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
location /private/question-service/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
location /private/user-service/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
location /public/collab-service/ {
location /public/collab-service/chat {
proxy_pass http://collab-service/chat;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /public/collab-service/yjs {
proxy_pass http://collab-service/yjs;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
location /public/matching-service/ {
location /public/matching-service/match/ {
location /public/matching-service/match/subscribe/ {
proxy_pass http://matching-service/match/subscribe/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
location /public/question-service/ {
location /public/question-service/questions {
limit_except GET {
deny all;
}
proxy_pass http://question-service/questions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
location /public/user-service/ {
location /public/user-service/auth {
proxy_pass http://user-service/auth;
}

location /public/user-service/users {
proxy_pass http://user-service/users;
}
}
31 changes: 31 additions & 0 deletions api-gateway/templates/default.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include conf.d/api_conf.d/api_backends.conf;

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

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

location /private/ {
auth_request /verify-token;
include conf.d/api_conf.d/private_conf.d/*.conf;
}

location /owner/ {
auth_request /verify-owner;
include conf.d/api_conf.d/owner_conf.d/*.conf;
}

location /admin/ {
auth_request /verify-admin;
include conf.d/api_conf.d/admin_conf.d/*.conf;
}

include conf.d/api_conf.d/auth.conf;

include conf.d/api_conf.d/frontend.conf;
}

include conf.d/map_conf.d/*.conf;
1 change: 1 addition & 0 deletions collab-service/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const server = http.createServer(index);
const docs = ywsUtils.docs;

const io = new Server(server, {
path: "/chat",
cors: {
origin: "*", // Allow all origins; replace with specific origin if needed
methods: ["GET", "POST"],
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ services:
collab-service:
build:
target: dev

# access RedisInsight at http://localhost:5540
# connect to redis on redis insight at redis:6379
redisinsight:
image: redis/redisinsight:latest
restart: always
ports:
- $REDIS_INSIGHT_PORT:$REDIS_INSIGHT_PORT # Expose RedisInsight UI on port 5540
depends_on:
- redis
67 changes: 35 additions & 32 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
services:
api-gateway:
image: nginx:1.26
volumes:
- ./api-gateway/templates:/etc/nginx/templates
- ./api-gateway/nginx.conf:/etc/nginx/nginx.conf
ports:
- $API_GATEWAY_PORT:$API_GATEWAY_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
depends_on:
- frontend
- user-service
- question-service
- matching-service
- collab-service

frontend:
build:
context: ./frontend
args:
- BASE_URI=$BASE_URI
- USER_SVC_PORT=$USER_SVC_PORT
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
- MATCHING_SVC_PORT=$MATCHING_SVC_PORT
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
ports:
- $FRONTEND_PORT:$FRONTEND_PORT
depends_on:
- question-service
- user-service
- collab-service
- API_GATEWAY_PORT=$API_GATEWAY_PORT
environment:
- PORT=$FRONTEND_PORT
expose:
- $FRONTEND_PORT

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

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

matching-service:
build:
context: ./matching-service
ports:
- $MATCHING_SVC_PORT:$MATCHING_SVC_PORT
environment:
- PORT=$MATCHING_SVC_PORT
- REDIS_HOST=redis
- REDIS_PORT=$REDIS_PORT
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
- COLLAB_SVC_PORT=$COLLAB_SVC_PORT
expose:
- $MATCHING_SVC_PORT
depends_on:
- redis
- question-service
Expand All @@ -58,26 +72,15 @@ services:
redis:
image: redis:7.4-alpine
restart: always
ports:
- $REDIS_PORT:$REDIS_PORT

# access RedisInsight at http://localhost:5540
# connect to redis on redis insight at redis:6379
redisinsight:
image: redis/redisinsight:latest
restart: always
ports:
- $REDIS_INSIGHT_PORT:$REDIS_INSIGHT_PORT # Expose RedisInsight UI on port 5540
depends_on:
- redis
expose:
- $REDIS_PORT


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

0 comments on commit 8e85ae9

Please sign in to comment.