Skip to content

Commit 7515751

Browse files
authored
Merge pull request #112 from CS3219-AY2425S1/cloud-fix
setup cors
2 parents 5fed02d + 6de1686 commit 7515751

File tree

8 files changed

+447
-1
lines changed

8 files changed

+447
-1
lines changed

api-gateway/src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const app = express();
1515

1616
// Middleware
1717
app.use(cors(config.corsOptions));
18+
app.options('*', cors(config.corsOptions)); // enable pre-flight for all routes
1819
app.use(express.json());
1920

2021
// Logging middleware

api-gateway/src/config/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import dotenv from 'dotenv';
22
import path from 'path';
3+
import logger from '../utils/logger';
34

45
// Load different env files based on NODE_ENV
56
const envFile = process.env.NODE_ENV === 'production' ? '.env' : '.env.dev';
67
dotenv.config({ path: path.resolve(process.cwd(), envFile) });
78

9+
const getAllowedOrigins = () => {
10+
if (process.env.NODE_ENV === 'production') {
11+
const frontendUrl = process.env.FRONTEND_URL;
12+
if (!frontendUrl) {
13+
logger.warn('FRONTEND_URL environment variable not set in production');
14+
return ['http://localhost:3000'];
15+
}
16+
// If frontendUrl already includes protocol, use it as is
17+
if (
18+
frontendUrl.startsWith('http://') ||
19+
frontendUrl.startsWith('https://')
20+
) {
21+
const httpVersion = frontendUrl;
22+
const httpsVersion = frontendUrl.replace('http://', 'https://');
23+
return [httpVersion, httpsVersion];
24+
}
25+
// Otherwise, add protocols (backward compatibility)
26+
return [`http://${frontendUrl}`, `https://${frontendUrl}`];
27+
}
28+
return ['http://localhost:3000'];
29+
};
30+
831
export default {
932
port: process.env.PORT || 8001,
1033
corsOptions: {
11-
origin: process.env.FRONTEND_URL || 'http://localhost:3000',
34+
origin: getAllowedOrigins(),
1235
credentials: true,
36+
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
37+
allowedHeaders: ['Content-Type', 'Authorization'],
1338
},
1439
services: {
1540
question: process.env.QUESTION_SERVICE_URL || 'http://localhost:4001',

cloudbuilds/api-gateway.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
steps:
2+
# Step 1: build and push all images to Artifact Registry
3+
- name: "gcr.io/cloud-builders/docker"
4+
args:
5+
[
6+
"build",
7+
"-t",
8+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest",
9+
"-f",
10+
"api-gateway/Dockerfile",
11+
"api-gateway",
12+
]
13+
14+
- name: "gcr.io/cloud-builders/docker"
15+
args:
16+
[
17+
"push",
18+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest",
19+
]
20+
21+
# Step 2: Clean up and deploy to GKE
22+
- name: "ubuntu"
23+
args: ["rm", "-rf", "output"]
24+
25+
- name: "gcr.io/cloud-builders/gke-deploy"
26+
args:
27+
- run
28+
- --filename=k8s/api-gateway.yml
29+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest
30+
- --location=asia-southeast1
31+
- --cluster=cs3219-g11-peerprep-kubes
32+
33+
# Step 3: update the deployments with the new images
34+
- name: "gcr.io/cloud-builders/kubectl"
35+
args:
36+
- "set"
37+
- "image"
38+
- "deployment/api-gateway"
39+
- "api-gateway=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest"
40+
env:
41+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
42+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
43+
44+
images:
45+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest"

cloudbuilds/cloudbuild.yaml

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
steps:
2+
# Step 1: build and push all images to Artifact Registry
3+
4+
# Build and push peerprep-fe
5+
- name: "gcr.io/cloud-builders/docker"
6+
args:
7+
[
8+
"build",
9+
"-t",
10+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest",
11+
"--build-arg",
12+
"NEXT_PUBLIC_API_GATEWAY_URL=${_NEXT_PUBLIC_API_GATEWAY_URL}",
13+
"--build-arg",
14+
"NEXT_PUBLIC_GITHUB_CLIENT_ID=${_NEXT_PUBLIC_GITHUB_CLIENT_ID}",
15+
"-f",
16+
"peerprep-fe/Dockerfile",
17+
"peerprep-fe",
18+
]
19+
20+
# Push the peerprep-fe image to Artifact Registry
21+
- name: "gcr.io/cloud-builders/docker"
22+
args:
23+
[
24+
"push",
25+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest",
26+
]
27+
28+
# Build and push question-service
29+
- name: "gcr.io/cloud-builders/docker"
30+
args:
31+
[
32+
"build",
33+
"-t",
34+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/question-svc:latest",
35+
"-f",
36+
"question-service/Dockerfile",
37+
"question-service",
38+
]
39+
40+
# Push the question-service image to Artifact Registry
41+
- name: "gcr.io/cloud-builders/docker"
42+
args:
43+
[
44+
"push",
45+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/question-svc:latest",
46+
]
47+
48+
# Build and push user-service
49+
- name: "gcr.io/cloud-builders/docker"
50+
args:
51+
[
52+
"build",
53+
"-t",
54+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/user-svc:latest",
55+
"-f",
56+
"user-service/Dockerfile",
57+
"user-service",
58+
]
59+
60+
# Push the user-service image to Artifact Registry
61+
- name: "gcr.io/cloud-builders/docker"
62+
args:
63+
[
64+
"push",
65+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/user-svc:latest",
66+
]
67+
68+
# Build and push api-gateway
69+
- name: "gcr.io/cloud-builders/docker"
70+
args:
71+
[
72+
"build",
73+
"-t",
74+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest",
75+
"-f",
76+
"api-gateway/Dockerfile",
77+
"api-gateway",
78+
]
79+
80+
# Push the api-gateway image to Artifact Registry
81+
- name: "gcr.io/cloud-builders/docker"
82+
args:
83+
[
84+
"push",
85+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest",
86+
]
87+
88+
# Step 2: Clean up and deploy to GKE
89+
90+
# Clean up before deploying peerprep-fe
91+
- name: "ubuntu"
92+
args: ["rm", "-rf", "output"]
93+
94+
# Deploy peerprep-fe to Google Kubernetes Engine (GKE)
95+
- name: "gcr.io/cloud-builders/gke-deploy"
96+
args:
97+
- run
98+
- --filename=k8s/peerprep-fe.yml
99+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest
100+
- --location=asia-southeast1
101+
- --cluster=cs3219-g11-peerprep-kubes
102+
103+
# Clean up before deploying question-service
104+
- name: "ubuntu"
105+
args: ["rm", "-rf", "output"]
106+
107+
# Deploy question-service to Google Kubernetes Engine (GKE)
108+
- name: "gcr.io/cloud-builders/gke-deploy"
109+
args:
110+
- run
111+
- --filename=k8s/question-service.yml
112+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/question-svc:latest
113+
- --location=asia-southeast1
114+
- --cluster=cs3219-g11-peerprep-kubes
115+
116+
# Clean up before deploying user-service
117+
- name: "ubuntu"
118+
args: ["rm", "-rf", "output"]
119+
120+
# Deploy user-service to Google Kubernetes Engine (GKE)
121+
- name: "gcr.io/cloud-builders/gke-deploy"
122+
args:
123+
- run
124+
- --filename=k8s/user-service.yml
125+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/user-svc:latest
126+
- --location=asia-southeast1
127+
- --cluster=cs3219-g11-peerprep-kubes
128+
129+
# Clean up before deploying api-gateway
130+
- name: "ubuntu"
131+
args: ["rm", "-rf", "output"]
132+
133+
# Deploy api-gateway to Google Kubernetes Engine (GKE)
134+
- name: "gcr.io/cloud-builders/gke-deploy"
135+
args:
136+
- run
137+
- --filename=k8s/api-gateway.yml
138+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest
139+
- --location=asia-southeast1
140+
- --cluster=cs3219-g11-peerprep-kubes
141+
142+
# Step 3: update the deployments with the new images
143+
144+
# Update the peerprep-fe deployment
145+
- name: "gcr.io/cloud-builders/kubectl"
146+
args:
147+
- "set"
148+
- "image"
149+
- "deployment/peerprep-fe"
150+
- "peerprep-fe=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest"
151+
env:
152+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
153+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
154+
155+
# Update the question-service deployment
156+
- name: "gcr.io/cloud-builders/kubectl"
157+
args:
158+
- "set"
159+
- "image"
160+
- "deployment/question-svc"
161+
- "question-svc=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/question-svc:latest"
162+
env:
163+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
164+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
165+
166+
# Update the user-service deployment hi
167+
- name: "gcr.io/cloud-builders/kubectl"
168+
args:
169+
- "set"
170+
- "image"
171+
- "deployment/user-svc"
172+
- "user-svc=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/user-svc:latest"
173+
env:
174+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
175+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
176+
177+
# Update the api-gateway deployment
178+
- name: "gcr.io/cloud-builders/kubectl"
179+
args:
180+
- "set"
181+
- "image"
182+
- "deployment/api-gateway"
183+
- "api-gateway=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest"
184+
env:
185+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
186+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
187+
188+
substitutions:
189+
_NEXT_PUBLIC_API_GATEWAY_URL: ""
190+
_NEXT_PUBLIC_GITHUB_CLIENT_ID: ""
191+
192+
options:
193+
dynamic_substitutions: true
194+
195+
images:
196+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest"
197+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/question-svc:latest"
198+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/user-svc:latest"
199+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/api-gateway:latest"

cloudbuilds/fe.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
steps:
2+
# Step 1: build and push all images to Artifact Registry
3+
4+
# Build and push peerprep-fe
5+
- name: "gcr.io/cloud-builders/docker"
6+
args:
7+
[
8+
"build",
9+
"-t",
10+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest",
11+
"--build-arg",
12+
"NEXT_PUBLIC_API_GATEWAY_URL=${_NEXT_PUBLIC_API_GATEWAY_URL}",
13+
"--build-arg",
14+
"NEXT_PUBLIC_GITHUB_CLIENT_ID=${_NEXT_PUBLIC_GITHUB_CLIENT_ID}",
15+
"-f",
16+
"peerprep-fe/Dockerfile",
17+
"peerprep-fe",
18+
]
19+
20+
# Push the peerprep-fe image to Artifact Registry
21+
- name: "gcr.io/cloud-builders/docker"
22+
args:
23+
[
24+
"push",
25+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest",
26+
]
27+
28+
# Step 2: Clean up and deploy to GKE
29+
- name: "ubuntu"
30+
args: ["rm", "-rf", "output"]
31+
32+
# Deploy peerprep-fe to Google Kubernetes Engine (GKE)
33+
- name: "gcr.io/cloud-builders/gke-deploy"
34+
args:
35+
- run
36+
- --filename=k8s/peerprep-fe.yml
37+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest
38+
- --location=asia-southeast1
39+
- --cluster=cs3219-g11-peerprep-kubes
40+
41+
# Step 3: update the deployments with the new images
42+
- name: "gcr.io/cloud-builders/kubectl"
43+
args:
44+
- "set"
45+
- "image"
46+
- "deployment/peerprep-fe"
47+
- "peerprep-fe=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest"
48+
env:
49+
- "CLOUDSDK_COMPUTE_ZONE=asia-southeast1"
50+
- "CLOUDSDK_CONTAINER_CLUSTER=cs3219-g11-peerprep-kubes"
51+
52+
substitutions:
53+
_NEXT_PUBLIC_API_GATEWAY_URL: ""
54+
_NEXT_PUBLIC_GITHUB_CLIENT_ID: ""
55+
56+
options:
57+
dynamic_substitutions: true
58+
59+
images:
60+
- "asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/peerprep-fe:latest"

cloudbuilds/matching-service.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
steps:
2+
- name: "gcr.io/cloud-builders/docker"
3+
args:
4+
[
5+
"build",
6+
"-t",
7+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/matching-svc:latest",
8+
"-f",
9+
"matching-service/Dockerfile",
10+
"matching-service",
11+
]
12+
13+
- name: "gcr.io/cloud-builders/docker"
14+
args:
15+
[
16+
"push",
17+
"asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/matching-svc:latest",
18+
]
19+
20+
- name: "gcr.io/cloud-builders/gke-deploy"
21+
args:
22+
- run
23+
- --filename=k8s/matching-service.yml
24+
- --image=asia-southeast1-docker.pkg.dev/cs3219-g11-peerprep/cs3219-g11-repo/matching-svc:latest
25+
- --location=asia-southeast1
26+
- --cluster=cs3219-g11-peerprep-kubes

0 commit comments

Comments
 (0)