Skip to content

Commit 590b0b4

Browse files
committed
Dockerise nginx and add basic nginx.conf
1 parent e986eaf commit 590b0b4

File tree

10 files changed

+100
-103
lines changed

10 files changed

+100
-103
lines changed

compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ services:
132132
build: nginx
133133
ports:
134134
- "80:80"
135+
volumes:
136+
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
137+
depends_on:
138+
- peerprep
139+
- backend
140+
- user-service
141+
- storage-blob-api
142+
- matching-service-api
135143
# mongo:
136144
# image: "mongo:latest"
137145
# ports:

nginx/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
### So far local testing completed:
1+
nginx is dockerised, just have to run `docker compose up --build` as usual.
22

3-
1. Run frontend/backend services (front 3000, qn 9090, user 3001)
4-
2. copy nginx.conf into ur local install location
5-
3. start nginx
6-
4. go to localhost and should hopefully see the site
3+
if edits are made to the local nginx.conf file, following command must be run to see changes reflected in docker:
74

8-
### TODO:
5+
`docker exec cs3219-ay2425s1-project-g14-nginx-1 nginx -s reload`
96

10-
- dockerize nginx
11-
- change config to work with dockerised services
7+
(or just exec `nginx -s reload` in the container directly)

nginx/nginx.conf

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# current sorry state of my nginx.conf file
21
worker_processes 1;
32

43
events {
@@ -7,65 +6,69 @@ events {
76

87
http {
98
include mime.types;
9+
1010
default_type application/octet-stream;
11-
# for caching of cookie
12-
proxy_cache_path cache/ keys_zone=auth_cache:1m;
1311

14-
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
15-
# '$status $body_bytes_sent "$http_referer" '
16-
# '"$http_user_agent" "$http_x_forwarded_for"';
17-
18-
#access_log logs/access.log main;
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"';
1915

2016
sendfile on;
2117
#tcp_nopush on;
2218

23-
#keepalive_timeout 0;
2419
keepalive_timeout 65;
2520

26-
#gzip on;
2721

28-
server {
29-
listen 80;
30-
server_name localhost;
3122

32-
# frontend (i guess login page)
33-
location / {
34-
proxy_pass http://peerprep:3000/; # frontend running on...similar for other services
35-
proxy_http_version 1.1;
36-
proxy_set_header Upgrade $http_upgrade;
37-
proxy_set_header Connection "Upgrade";
38-
proxy_set_header Host $host;
39-
proxy_cache_bypass $http_upgrade;
40-
}
23+
upstream peerprep {
24+
server peerprep:3000;
25+
}
4126

42-
# question
43-
location ~ ^/api/(?!internal).*$ {
44-
proxy_pass http://backend:9090/;
45-
proxy_set_header Host $host;
46-
proxy_set_header X-Real-IP $remote_addr;
47-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
48-
proxy_set_header X-Forwarded-Proto $scheme;
27+
upstream user_service {
28+
server user-service:3001;
29+
}
4930

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/;
5051
}
5152

52-
# user (pure conjecture, just referencing gaylord article)
5353
location /users/ {
54-
proxy_pass http://user-service:3001/;
55-
# auth_request auth;
56-
# auth_request_set $userid $upstream_http_x_auth_user;
57-
58-
# # if fail go back to landing
59-
# if ($http_accept ~* "text/html" ) {
60-
# error_page 401 403 =200 /;
61-
# }
54+
proxy_pass http://user_service/;
6255

63-
proxy_set_header X-Auth-User $userid;
64-
proxy_set_header Host $host;
65-
proxy_set_header X-Forwarded-For $remote_addr;
56+
}
6657

58+
location /backend/ {
59+
proxy_pass http://backend/;
60+
}
6761

62+
location /matchmaking/ {
63+
proxy_pass http://matching_service_api/;
6864
}
6965

66+
location /blob/ {
67+
proxy_pass http://storage_blob_api/;
68+
}
69+
70+
location /collab/ {
71+
proxy_pass http://collab/;
72+
}
7073
}
7174
}

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/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) {

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { QuestionFullBody, StatusBody } from "@/api/structs";
22

33
export async function deleteQuestion(id: number): Promise<StatusBody> {
4-
const res = await fetch(
5-
`${process.env.NEXT_PUBLIC_NGINX}/api/internal/questions`,
6-
{
7-
method: "DELETE",
8-
body: JSON.stringify({ qid: id }),
9-
},
10-
);
4+
const res = await fetch(`/api/internal/questions`, {
5+
method: "DELETE",
6+
body: JSON.stringify({ qid: id }),
7+
});
118
if (res.ok) {
129
return { status: res.status };
1310
}
@@ -16,17 +13,14 @@ export async function deleteQuestion(id: number): Promise<StatusBody> {
1613
}
1714

1815
export async function addQuestion(
19-
question: QuestionFullBody,
16+
question: QuestionFullBody
2017
): Promise<StatusBody> {
2118
// TODO: this is not desired
2219
question.content = "<p>" + question.content + "</p>";
23-
const res = await fetch(
24-
`${process.env.NEXT_PUBLIC_NGINX}/api/internal/questions`,
25-
{
26-
method: "POST",
27-
body: JSON.stringify(question),
28-
},
29-
);
20+
const res = await fetch(`/api/internal/questions`, {
21+
method: "POST",
22+
body: JSON.stringify(question),
23+
});
3024
if (!res.ok) {
3125
return { status: res.status };
3226
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from "next/server";
44
export async function GET() {
55
try {
66
const response = await fetch(
7-
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
7+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
88
{
99
method: "GET",
1010
headers: generateAuthHeaders(),
@@ -32,7 +32,7 @@ export async function POST(request: NextRequest) {
3232
const body = await request.json();
3333
try {
3434
const response = await fetch(
35-
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
35+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
3636
{
3737
method: "POST",
3838
body: JSON.stringify(body),
@@ -70,7 +70,7 @@ export async function DELETE(request: NextRequest) {
7070
}
7171
try {
7272
const response = await fetch(
73-
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/delete/${body.qid}`,
73+
`${process.env.NEXT_PUBLIC_NGINX}/${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/delete/${body.qid}`,
7474
{
7575
method: "DELETE",
7676
headers: generateAuthHeaders(),

peerprep/components/questionpage/QuestionList.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const QuestionList: React.FC = () => {
1919

2020
useEffect(() => {
2121
const fetchQuestions = async () => {
22-
const payload = await fetch(
23-
`${process.env.NEXT_PUBLIC_NGINX}/api/internal/questions`
24-
).then((res) => res.json());
22+
const payload = await fetch(`/api/internal/questions`).then((res) =>
23+
res.json()
24+
);
2525
// uh
2626
if (isError(payload)) {
2727
// should also reflect the error

0 commit comments

Comments
 (0)