Skip to content

Commit 704fdbc

Browse files
committed
Merge branch 'n9-api-gateway' of https://github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g14 into n9-api-gateway
2 parents 72ed9ec + 1b06717 commit 704fdbc

File tree

15 files changed

+160
-59
lines changed

15 files changed

+160
-59
lines changed

compose.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ services:
134134
- action: rebuild
135135
path: collab
136136
target: collab/app
137+
138+
nginx:
139+
build: nginx
140+
ports:
141+
- "80:80"
142+
volumes:
143+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
144+
depends_on:
145+
- peerprep
146+
- backend
147+
- user-service
148+
- storage-blob-api
149+
- matching-service-api
137150
# mongo:
138151
# image: "mongo:latest"
139152
# ports:

nginx/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM nginx:alpine
2+
COPY nginx.conf /etc/nginx/nginx.conf
3+
EXPOSE 80
4+
CMD ["nginx", "-g", "daemon off;"]

nginx/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nginx is dockerised, just have to run `docker compose up --build` as usual.
2+
3+
if edits are made to the local nginx.conf file, following command must be run to see changes reflected in docker:
4+
5+
`docker exec cs3219-ay2425s1-project-g14-nginx-1 nginx -s reload`
6+
7+
(or just exec `nginx -s reload` in the container directly)

nginx/nginx.conf

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
worker_processes 1;
2+
3+
events {
4+
worker_connections 1024;
5+
}
6+
7+
http {
8+
include mime.types;
9+
10+
default_type application/octet-stream;
11+
12+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
13+
'$status $body_bytes_sent "$http_referer" '
14+
'"$http_user_agent" "$http_x_forwarded_for"';
15+
16+
sendfile on;
17+
#tcp_nopush on;
18+
19+
keepalive_timeout 65;
20+
21+
22+
23+
upstream peerprep {
24+
server peerprep:3000;
25+
}
26+
27+
upstream user_service {
28+
server user-service:3001;
29+
}
30+
31+
upstream backend {
32+
server backend:9090;
33+
}
34+
35+
upstream matching_service_api {
36+
server matching-service-api:9200;
37+
}
38+
39+
upstream storage_blob_api {
40+
server storage-blob-api:9300;
41+
}
42+
43+
# upstream collab {
44+
# server collab:4000;
45+
# }
46+
47+
server {
48+
listen 80;
49+
location / {
50+
proxy_pass http://peerprep/;
51+
proxy_set_header Host $host;
52+
proxy_set_header X-Real-IP $remote_addr;
53+
proxy_set_header X-Forwarded-Proto $scheme;
54+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
55+
}
56+
57+
location /users/ {
58+
proxy_pass http://user_service/;
59+
60+
}
61+
62+
location /backend/ {
63+
proxy_pass http://backend/;
64+
}
65+
66+
location /matchmaking/ {
67+
proxy_pass http://matching_service_api/;
68+
}
69+
70+
location /blob/ {
71+
proxy_pass http://storage_blob_api/;
72+
}
73+
74+
# location /collab/ {
75+
# proxy_pass http://collab/;
76+
# }
77+
}
78+
}

peerprep/.env.example

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
NEXT_PUBLIC_BASE_URL=http://host.docker.internal:80
2-
NEXT_PUBLIC_QUESTION_SERVICE=http://backend:9090
3-
NEXT_PUBLIC_USER_SERVICE=http://user-service:3001
4-
# note NGINX will currently point to itself.
5-
NEXT_PUBLIC_NGINX=http://localhost:3000
1+
#NEXT_PUBLIC_BASE_URL=http://host.docker.internal:80
2+
NEXT_PUBLIC_QUESTION_SERVICE=backend
3+
NEXT_PUBLIC_USER_SERVICE=users
4+
5+
NEXT_PUBLIC_NGINX=http://host.docker.internal:80
66
DEV_ENV=not
7-
NEXT_PUBLIC_MATCHING_SERVICE=http://matching-service-api:9200
8-
NEXT_PUBLIC_STORAGE_BLOB=http://storage-blob-api:9300
7+
NEXT_PUBLIC_MATCHING_SERVICE=matchmaking
8+
NEXT_PUBLIC_STORAGE_BLOB=blob

peerprep/.env.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# THIS CANNOT BE RIGHT, TEMP FIX
2+
NEXT_PUBLIC_BASE_URL=http://localhost
3+
# location of question service
4+
NEXT_PUBLIC_QUESTION_SERVICE=api
5+
NEXT_PUBLIC_USER_SERVICE=users
6+
# dev flag, originally used when services were not up
7+
DEV_ENV=not

peerprep/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WORKDIR /frontend
44
# TODO: don't include the .env file in the COPY
55
# TODO: multistage build
66
COPY package*.json ./
7-
RUN npm install
7+
RUN npm install --force
88
COPY . .
99
EXPOSE 3000
1010
CMD ["npm", "run", "dev"]

peerprep/api/gateway.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function fetchQuestion(
2929
): Promise<Question | StatusBody> {
3030
try {
3131
const response = await fetch(
32-
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`,
32+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`,
3333
{
3434
method: "GET",
3535
headers: generateAuthHeaders(),
@@ -58,7 +58,7 @@ export async function getSessionLogin(validatedFields: {
5858
}): Promise<LoginResponse | StatusBody> {
5959
try {
6060
const res = await fetch(
61-
`${process.env.NEXT_PUBLIC_USER_SERVICE}/auth/login`,
61+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_USER_SERVICE}/auth/login`,
6262
{
6363
method: "POST",
6464
body: JSON.stringify(validatedFields),
@@ -87,13 +87,16 @@ export async function postSignupUser(validatedFields: {
8787
}): Promise<UserServiceResponse | StatusBody> {
8888
try {
8989
console.log(JSON.stringify(validatedFields));
90-
const res = await fetch(`${process.env.NEXT_PUBLIC_USER_SERVICE}/users`, {
91-
method: "POST",
92-
body: JSON.stringify(validatedFields),
93-
headers: {
94-
"Content-type": "application/json; charset=UTF-8",
95-
},
96-
});
90+
const res = await fetch(
91+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_USER_SERVICE}/users`,
92+
{
93+
method: "POST",
94+
body: JSON.stringify(validatedFields),
95+
headers: {
96+
"Content-type": "application/json; charset=UTF-8",
97+
},
98+
}
99+
);
97100
const json = await res.json();
98101

99102
if (!res.ok) {
@@ -110,7 +113,7 @@ export async function postSignupUser(validatedFields: {
110113
export async function verifyUser(): Promise<UserServiceResponse | StatusBody> {
111114
try {
112115
const res = await fetch(
113-
`${process.env.NEXT_PUBLIC_USER_SERVICE}/auth/verify-token`,
116+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_USER_SERVICE}/auth/verify-token`,
114117
{
115118
method: "GET",
116119
headers: generateAuthHeaders(),

peerprep/app/api/internal/matching/helper.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ export async function checkMatchStatus(
1010
userId: string
1111
): Promise<MatchResponse | StatusBody> {
1212
console.debug("In matching helper, checking storage blob:", userId);
13-
const res = await fetch(
14-
`${process.env.NEXT_PUBLIC_NGINX}/api/internal/matching?uid=${userId}`,
15-
{
16-
method: "GET",
17-
}
18-
);
13+
const res = await fetch(`/api/internal/matching?uid=${userId}`, {
14+
method: "GET",
15+
});
1916
if (!res.ok) {
2017
return {
2118
error: await res.text(),
@@ -38,13 +35,10 @@ export async function findMatch(
3835
"In matching helper, posting match request",
3936
JSON.stringify(matchRequest)
4037
);
41-
const res = await fetch(
42-
`${process.env.NEXT_PUBLIC_NGINX}/api/internal/matching`,
43-
{
44-
method: "POST",
45-
body: JSON.stringify(matchRequest),
46-
}
47-
);
38+
const res = await fetch(`/api/internal/matching`, {
39+
method: "POST",
40+
body: JSON.stringify(matchRequest),
41+
});
4842
if (!res.ok) {
4943
return {
5044
error: await res.text(),

peerprep/app/api/internal/matching/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function GET(request: NextRequest) {
1212

1313
try {
1414
const response = await fetch(
15-
`${process.env.NEXT_PUBLIC_STORAGE_BLOB}/request/${uid}`,
15+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_STORAGE_BLOB}/request/${uid}`,
1616
{
1717
method: "GET",
1818
headers: generateAuthHeaders(),
@@ -41,11 +41,10 @@ export async function POST(request: NextRequest) {
4141
const body = await request.json();
4242
try {
4343
const response = await fetch(
44-
`${process.env.NEXT_PUBLIC_MATCHING_SERVICE}/request`,
44+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_MATCHING_SERVICE}/request`,
4545
{
4646
method: "POST",
4747
body: JSON.stringify(body),
48-
headers: generateJSONHeaders(),
4948
}
5049
);
5150
if (response.ok) {

0 commit comments

Comments
 (0)