Skip to content

Commit

Permalink
Merge pull request #51 from CS3219-AY2425S1/deployment-workings
Browse files Browse the repository at this point in the history
NGINX Configurations for API Gateway, Code Updates for BE and FE Deployment
  • Loading branch information
smolegz authored Nov 7, 2024
2 parents b82f153 + bcc1f58 commit feb1845
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 29 deletions.
8 changes: 8 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
runtime: nodejs16
env: standard
handlers:
- url: /
static_files: dist/peer-prep-fe/browser/index.html
upload: dist/peer-prep-fe/browser/index.html
- url: /
static_dir: dist/peer-prep-fe/browser
4 changes: 2 additions & 2 deletions code-websocket/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 8081 });
const server = new WebSocket.Server({ port: process.env.PORT || 8081 });

// Store the code for each session ID
const sessionData = {};
Expand Down Expand Up @@ -89,4 +89,4 @@ function broadcastToSession(sessionID, message, excludeSocket = null) {
});
}

console.log('WebSocket server is running on ws://localhost:8081');
console.log(`WebSocket server is running on ws://localhost:${process.env.PORT || 8081}`);
87 changes: 87 additions & 0 deletions deploy_microservices.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash

echo "------------------------------------------------------------------------------"
echo "Building and deploying peer-prep-question..."
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/peer-prep-question ./peer-prep-be
docker tag gcr.io/g01-peer-prep/peer-prep-question:latest gcr.io/g01-peer-prep/peer-prep-question:latest
docker push gcr.io/g01-peer-prep/peer-prep-question:latest
gcloud run deploy peer-prep-question \
--image gcr.io/g01-peer-prep/peer-prep-question:latest \
--region asia-southeast1 \
--allow-unauthenticated \
--set-env-vars "DB_URL=mongodb+srv://peer-prep:[email protected]/?retryWrites=true&w=majority&appName=peer-prep-cluster0"
echo "peer-prep-question deployed successfully."
echo "------------------------------------------------------------------------------"

echo "------------------------------------------------------------------------------"
echo "Building and deploying peer-prep-user..."
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/peer-prep-user ./peer-prep-user/user-service
docker tag gcr.io/g01-peer-prep/peer-prep-user:latest gcr.io/g01-peer-prep/peer-prep-user:latest
docker push gcr.io/g01-peer-prep/peer-prep-user:latest
gcloud run deploy peer-prep-user \
--image gcr.io/g01-peer-prep/peer-prep-user:latest \
--region asia-southeast1 \
--allow-unauthenticated \
--set-env-vars "ENV=PROD,DB_CLOUD_URI=mongodb+srv://peer-prep:[email protected]/?retryWrites=true&w=majority&appName=peer-prep-cluster0,JWT_SECRET=you-can-replace-this-with-your-own-secret"
echo "peer-prep-user deployed successfully."
echo "------------------------------------------------------------------------------"

echo "------------------------------------------------------------------------------"
echo "Building and deploying peer-prep-matching..."
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/rabbitmq ./message_queue
docker tag gcr.io/g01-peer-prep/rabbitmq:latest gcr.io/g01-peer-prep/rabbitmq:latest
docker push gcr.io/g01-peer-prep/rabbitmq:latest
gcloud run deploy peer-prep-matching \
--image gcr.io/g01-peer-prep/peer-prep-matching:latest \
--region asia-southeast1 \
--allow-unauthenticated \
--set-env-vars "AMQP_SERVER=amqps://lguugvwb:[email protected]/lguugvwb,MONGODB_URI=mongodb+srv://peer-prep:[email protected]/?retryWrites=true&w=majority&appName=peer-prep-cluster0"
echo "peer-prep-matching deployed successfully."
echo "------------------------------------------------------------------------------"

echo "------------------------------------------------------------------------------"
echo "Building and deploying peer-prep-collab..."
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/peer-prep-collab ./peer-prep-collab
docker tag gcr.io/g01-peer-prep/peer-prep-collab:latest gcr.io/g01-peer-prep/peer-prep-collab:latest
docker push gcr.io/g01-peer-prep/peer-prep-collab:latest
gcloud run deploy peer-prep-collab \
--image gcr.io/g01-peer-prep/peer-prep-collab:latest \
--region asia-southeast1 \
--allow-unauthenticated \
--set-env-vars "AMQP_SERVER=amqps://lguugvwb:[email protected]/lguugvwb,MATCHING_SERVICE_URL=https://peer-prep-matching-1093398872288.asia-southeast1.run.app,QUESTIONS_SERVICE_URL=https://peer-prep-question-1093398872288.asia-southeast1.run.app"
echo "peer-prep-collab deployed successfully."
echo "------------------------------------------------------------------------------"

echo "------------------------------------------------------------------------------"
echo "Building and deploying collab-websocket..."
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/collab-websocket ./code-websocket
docker tag gcr.io/g01-peer-prep/collab-websocket:latest gcr.io/g01-peer-prep/collab-websocket:latest
docker push gcr.io/g01-peer-prep/collab-websocket:latest
gcloud run deploy collab-websocket \
--image gcr.io/g01-peer-prep/collab-websocket:latest \
--region asia-southeast1 \
--allow-unauthenticated
echo "collab-websocket deployed successfully."
echo "------------------------------------------------------------------------------"

echo "------------------------------------------------------------------------------"
echo "Building and dploying peer-prep-gateway"
docker build --platform linux/amd64 -t gcr.io/g01-peer-prep/peer-prep-gateway -f ./peer-prep-gateway/Dockerfile .
docker tag gcr.io/g01-peer-prep/peer-prep-gateway:latest gcr.io/g01-peer-prep/peer-prep-gateway:latest
docker push gcr.io/g01-peer-prep/peer-prep-gateway:latest
gcloud run deploy peer-prep-gateway \
--image gcr.io/g01-peer-prep/peer-prep-gateway:latest \
--region asia-southeast1 \
--allow-unauthenticated
echo "peer-prep-gateway deployed successfully."
echo "------------------------------------------------------------------------------"

echo "All microservices have been deployed, check for errors."

# Easy access to the deployed services:
# Question Service: https://peer-prep-question-1093398872288.asia-southeast1.run.app
# User Service: https://peer-prep-user-1093398872288.asia-southeast1.run.app
# Matching Service: https://peer-prep-matching-1093398872288.asia-southeast1.run.app
# Collaboration Service: https://peer-prep-collab-1093398872288.asia-southeast1.run.app
# WebSocket Service: https://collab-websocket-1093398872288.asia-southeast1.run.app
# Gateway Service: https://peer-prep-gateway-1093398872288.asia-southeast1.run.app
17 changes: 16 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,22 @@ services:
ports:
- "8081:8081"
networks:
- ihatefrontend
- ihatefrontend

nginx:
image: nginx:alpine
container_name: peer-prep-gateway
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- question
- user
- collaboration
- websocket
networks:
- ihatefrontend

networks:
rabbitmqNetwork:
Expand Down
7 changes: 4 additions & 3 deletions message-queue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ app.post("/match", async (req: Request, res: Response) => {
}
})

app.listen(3002, () => {
console.log("Matching service running.")
})
const port = process.env.PORT || 3002;
app.listen(port, () => {
console.log(`Matching service running on port ${port}.`);
});
133 changes: 133 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
events {
worker_connections 1024;
}

http {
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;
error_log /var/log/nginx/error.log warn;

upstream question_service {
server peer-prep-question-1093398872288.asia-southeast1.run.app;
}

upstream user_service {
server peer-prep-user-1093398872288.asia-southeast1.run.app;
}

upstream matching_service {
server peer-prep-matching-1093398872288.asia-southeast1.run.app;
}

upstream collab_service {
server peer-prep-collab-1093398872288.asia-southeast1.run.app;
}

upstream websocket_service {
server collab-websocket-1093398872288.asia-southeast1.run.app;
}

server {
listen 8080;

location /api/questions {
proxy_pass https://peer-prep-question-1093398872288.asia-southeast1.run.app/questions;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host peer-prep-question-1093398872288.asia-southeast1.run.app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /api/questions/ {
proxy_pass https://peer-prep-question-1093398872288.asia-southeast1.run.app/questions/;
proxy_set_header Host peer-prep-question-1093398872288.asia-southeast1.run.app;
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 /api/categories {
proxy_pass https://peer-prep-question-1093398872288.asia-southeast1.run.app/categories;
proxy_set_header Host peer-prep-question-1093398872288.asia-southeast1.run.app;
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 /api/categories/ {
proxy_pass https://peer-prep-question-1093398872288.asia-southeast1.run.app/categories/;
proxy_set_header Host peer-prep-question-1093398872288.asia-southeast1.run.app;
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 /api/auth {
proxy_pass https://peer-prep-user-1093398872288.asia-southeast1.run.app/auth;
proxy_set_header Host peer-prep-user-1093398872288.asia-southeast1.run.app;
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 /api/auth/ {
proxy_pass https://peer-prep-user-1093398872288.asia-southeast1.run.app/auth/;
proxy_set_header Host peer-prep-user-1093398872288.asia-southeast1.run.app;
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 /api/users {
proxy_pass https://peer-prep-user-1093398872288.asia-southeast1.run.app/users;
proxy_set_header Host peer-prep-user-1093398872288.asia-southeast1.run.app;
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 /api/users/ {
proxy_pass https://peer-prep-user-1093398872288.asia-southeast1.run.app/users/;
proxy_set_header Host peer-prep-user-1093398872288.asia-southeast1.run.app;
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 /api/match {
proxy_pass https://peer-prep-matching-1093398872288.asia-southeast1.run.app/match;
proxy_set_header Host peer-prep-matching-1093398872288.asia-southeast1.run.app;
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 /api/collab {
proxy_pass https://peer-prep-collab-1093398872288.asia-southeast1.run.app/collab;
proxy_set_header Host peer-prep-collab-1093398872288.asia-southeast1.run.app;
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 /websocket/ {
proxy_pass https://collab-websocket-1093398872288.asia-southeast1.run.app/;
proxy_set_header Host collab-websocket-1093398872288.asia-southeast1.run.app;
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;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
add_header Access-Control-Allow-Origin https://g01-peer-prep.vercel.app;
}

error_page 404 /404.html;
location = /404.html {
internal;
}
}
}
5 changes: 5 additions & 0 deletions peer-prep-be/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ WORKDIR /root/
# Copy the Go binary from the first stage
COPY --from=builder /app/test_run .

# Uncomment when deploying question service. Comment back before any commit.
# COPY .env .

ENV PORT=8080

# Expose the application's port
EXPOSE 8080

Expand Down
15 changes: 14 additions & 1 deletion peer-prep-be/src/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g01/peer-prep-be/src/configs"
"github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g01/peer-prep-be/src/routes"
Expand All @@ -11,7 +13,10 @@ import (

func main() {
//run database
fmt.Println("Starting server...")
fmt.Println("Attempting to connect to MongoDB...")
configs.ConnectDB()
fmt.Println("Connected to MongoDB")

e := echo.New()
// Middleware
Expand All @@ -29,5 +34,13 @@ func main() {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":8080"))

// Get port from environment variable or default to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}

// Start the server on the specified port
e.Logger.Fatal(e.Start(":" + port))
}
2 changes: 1 addition & 1 deletion peer-prep-collab/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ COPY . .
EXPOSE 4003

# Start the application
# CMD ["npm", "start"]
CMD ["npm", "start"]

# docker build -t peer-prep-collab .
# docker run -p 4003:4003 peer-prep-collab
2 changes: 1 addition & 1 deletion peer-prep-collab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ADD_SESSION_QUEUE = 'add_session_queue'

export const connectToRabbitMQ = async () => {
try {
const amqpServer = "amqps://lguugvwb:[email protected]/lguugvwb"
const amqpServer = process.env.AMQP_SERVER || "amqps://lguugvwb:[email protected]/lguugvwb";
const connection = await amqp.connect(amqpServer)
channel = await connection.createChannel()
console.log("Connected to RabbitMQ")
Expand Down
1 change: 1 addition & 0 deletions peer-prep-fe/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ Thumbs.db
# Angular development server
/.angular/

.vercel
11 changes: 11 additions & 0 deletions peer-prep-fe/.vercel/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
> Why do I have a folder named ".vercel" in my project?
The ".vercel" folder is created when you link a directory to a Vercel project.

> What does the "project.json" file contain?
The "project.json" file contains:
- The ID of the Vercel project that you linked ("projectId")
- The ID of the user or team your Vercel project is owned by ("orgId")

> Should I commit the ".vercel" folder?
No, you should not share the ".vercel" folder with anyone.
Upon creation, it will be automatically added to your ".gitignore" file.
1 change: 1 addition & 0 deletions peer-prep-fe/.vercel/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"orgId":"team_GKhx06Xr5YsGKwEBsAosQhAU","projectId":"prj_22lBfKTHAtN1UMmfZdATwzmnDCtp"}
1 change: 1 addition & 0 deletions peer-prep-fe/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baseUrlProduction = "https://peer-prep-gateway-1093398872288.asia-southeast1.run.app/api";
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Validators
} from "@angular/forms"
import { Router } from "@angular/router"
import { baseUrlProduction } from "../../../constants"

import { authService } from "../authService/authService"

Expand Down Expand Up @@ -62,9 +63,12 @@ export class CreateAccountComponent {
// signInWithGoogle() {
// this.authService.login();
// }
isProduction(): boolean {
return window.location.hostname !== "localhost"
}

createAccount() {
let apiUrl: string = "http://localhost:3001/users"
let apiUrl: string = this.isProduction() ? `${baseUrlProduction}/users` : "http://localhost:3001/users"

if (this.createAccountForm.invalid) {
return
Expand Down
Loading

0 comments on commit feb1845

Please sign in to comment.