Skip to content

Commit

Permalink
Merge pull request #186 from CS3219-AY2425S1/184-nginx-for-collab-ser…
Browse files Browse the repository at this point in the history
…vice

Update NGINX for Collaboration Service
  • Loading branch information
Daviancold authored Nov 3, 2024
2 parents 8c807f4 + b38a868 commit 58fc6eb
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
dockerfile: ./backend/question-service/Dockerfile
- image: glemenneo/cs3219-ay2425s1-project-g31-matching-service
dockerfile: ./backend/matching-service/Dockerfile
- image: glemenneo/cs3219-ay2425s1-project-g31-collaboration-service
dockerfile: ./backend/collaboration-service/Dockerfile
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
dockerfile: ./backend/question-service/Dockerfile
- image: glemenneo/cs3219-ay2425s1-project-g31-matching-service
dockerfile: ./backend/matching-service/Dockerfile
- image: glemenneo/cs3219-ay2425s1-project-g31-collaboration-service
dockerfile: ./backend/collaboration-service/Dockerfile
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion backend/collaboration-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"reflect-metadata": "^0.2.2",
"socket.io": "^4.8.1",
"winston": "^3.14.2",
"y-websocket": "^2.0.4"
"y-websocket": "^2.0.4",
"ws": "^8.18.0"
},
"devDependencies": {
"@repo/eslint-config": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function submitCode(request: ITypedBodyRequest<SubmissionRequestDto
return
}

const res = await judgeZero.post(config.JUDGE_ZERO_SUBMIT_CONFIG, submissionRequestDto)
const res = await judgeZero.post(config.JUDGE_ZERO_SUBMIT_CONFIG, request.body)

if (!res) {
response.status(400).json('Failed to submit code. Please try again.').send()
Expand All @@ -87,7 +87,7 @@ export async function submitCode(request: ITypedBodyRequest<SubmissionRequestDto
const responseErrors = await submissionResponseDto.validate()

if (responseErrors.length) {
const errorMessages = requestErrors.flatMap((error: ValidationError) => Object.values(error.constraints))
const errorMessages = responseErrors.flatMap((error: ValidationError) => Object.values(error.constraints))
response.status(400).json(errorMessages).send()
return
}
Expand Down
16 changes: 11 additions & 5 deletions backend/collaboration-service/src/services/judgezero.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosInstance, AxiosResponse } from 'axios'
import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import config from '../common/config.util'
import { SubmissionRequestDto } from '@repo/submission-types'
import logger from '../common/logger.util'
Expand All @@ -15,10 +15,16 @@ class JudgeZero {
})

// Request Interceptor
this.axiosInstance.interceptors.request.use((error) => {
logger.error(`[Judge-Zero] Failed to send Judge Zero API request: ${error}`)
return Promise.reject(error)
})
this.axiosInstance.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
console.log(`Requesting [${config.method?.toUpperCase()}] ${config.baseURL}${config.url}`)
return config
},
(error) => {
logger.error(`[Judge-Zero] Failed to send Judge Zero API request: ${error}`)
return Promise.reject(error)
}
)

// Response Interceptor
this.axiosInstance.interceptors.response.use(
Expand Down
8 changes: 5 additions & 3 deletions backend/matching-service/__mocks__/config.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const { publicKey, privateKey } = generateKeyPairSync('rsa', {

export default {
NODE_ENV: 'test',
PORT: '8000',
PORT: '3006',
ACCESS_TOKEN_PUBLIC_KEY: Buffer.from(publicKey).toString('base64'),
ACCESS_TOKEN_PRIVATE_KEY: Buffer.from(privateKey).toString('base64'),
DB_URL: 'mongodb://localhost:27017/matching-service',
USER_SERVICE_URL: 'http://localhost:3002',
QUESTION_SERVICE_URL: 'http://localhost:3004',
RMQ_USER: 'test',
RMQ_PASSWORD: 'test',
RMQ_HOST: 'test',
DB_URL: 'test',
RMQ_HOST: 'localhost',
}
18 changes: 18 additions & 0 deletions backend/matching-service/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import request from 'supertest'
import configMock from '../__mocks__/config.mock'
import app from '../src/index'

jest.mock('../src/common/config.util', () => configMock)
jest.mock('../src/server.ts', () => {
wsConnection: {
}
})

describe('Index', () => {
describe('GET /', () => {
it('should return 200 OK', async () => {
const response = await request(app).get('/')
expect(response.status).toBe(200)
})
})
})
103 changes: 54 additions & 49 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
- USER_SERVICE_URL=http://user-service:3002
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- COLLABORATION_SERVICE_URL=http://collaboration-service:3008
- RMQ_URL=http://rabbitmq:15672
- DOMAIN_NAME=${DOMAIN_NAME}
image: nginx:1.27.2-alpine
Expand All @@ -22,98 +23,102 @@ services:
- matching-service
command: sh -c "envsubst < /etc/nginx/templates/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"

# collaboration-service:
# container_name: collaboration-service
# port:
# - 3008:3008
# build:
# context: .
# dockerfile: ./backend/collaboration-service/Dockerfile
# environment:
# - NODE_ENV=development
# - PORT=3006
# - DB_URL=mongodb://${MONGO_USER}:${MONGO_PASSWORD}@collab-db:27017/collaboration-service?authSource=admin
# - ACCESS_TOKEN_PUBLIC_KEY=${ACCESS_TOKEN_PUBLIC_KEY}
# - ACCESS_TOKEN_PRIVATE_KEY=${ACCESS_TOKEN_PRIVATE_KEY}
# - USER_SERVICE_URL=http://user-service:3002
# - QUESTION_SERVICE_URL=http://question-service:3004
# - MATCHING_SERVICE_URL=http://matching-service:3006
# - JUDGE_ZERO_URL=http://judgezero-server:2358
# - JUDGE_ZERO_SUBMIT_CONFIG=${JUDGE_ZERO_SUBMIT_CONFIG}
# restart: always
# networks:
# - backend-network
# depends_on:
# - judgezero-server
# - judgezero-workers
# - collab-db
collaboration-service:
container_name: collaboration-service
build:
context: .
dockerfile: ./backend/collaboration-service/Dockerfile
environment:
- NODE_ENV=development
- PORT=3008
- DB_URL=mongodb://${MONGO_USER}:${MONGO_PASSWORD}@collab-db:27017/collaboration-service?authSource=admin
- ACCESS_TOKEN_PUBLIC_KEY=${ACCESS_TOKEN_PUBLIC_KEY}
- ACCESS_TOKEN_PRIVATE_KEY=${ACCESS_TOKEN_PRIVATE_KEY}
- USER_SERVICE_URL=http://user-service:3002
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- JUDGE_ZERO_URL=http://judgezero-server:2358
- JUDGE_ZERO_SUBMIT_CONFIG=${JUDGE_ZERO_SUBMIT_CONFIG}
restart: always
networks:
- backend-network
depends_on:
- judgezero-server
- judgezero-workers
- collab-db

# collab-db:
# container_name: collab-db
# image: mongo:8.0.0
# environment:
# - MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}
# - MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
# ports:
# - '27087:27017'
# volumes:
# - 'collab_data:/data/db'
# networks:
# - backend-network
# command: mongod --quiet --logpath /dev/null --auth
collab-db:
container_name: collab-db
image: mongo:8.0.0
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
ports:
- '27087:27017'
volumes:
- 'collab_data:/data/db'
networks:
- backend-network
command: mongod --quiet --logpath /dev/null --auth

judgezero-server:
image: judge0/judge0:1.13.1
volumes:
- ./judge0.conf:/judge0.conf:ro
ports:
- 2358:2358
networks:
- backend-network
privileged: true
restart: always
depends_on:
judgezero-db:
db:
condition: service_healthy
judgezero-redis:
redis:
condition: service_healthy

judgezero-workers:
image: judge0/judge0:1.13.1
command: ["./scripts/workers"]
volumes:
- ./judge0.conf:/judge0.conf:ro
networks:
- backend-network
privileged: true
restart: always
depends_on:
judgezero-db:
db:
condition: service_healthy
judgezero-redis:
redis:
condition: service_healthy

judgezero-db:
db:
image: postgres:16.2
env_file: judge0.conf
volumes:
- judgezero_data:/var/lib/postgresql/data/
networks:
- backend-network
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
test: ["CMD-SHELL", "pg_isready -U judge0"]
interval: 10s
retries: 5
start_period: 5s
start_period: 10s

judgezero-redis:
redis:
image: redis:7.2.4
command: [
"bash", "-c",
'docker-entrypoint.sh --appendonly no --requirepass "$$REDIS_PASSWORD"'
]
env_file: judge0.conf
networks:
- backend-network
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
retries: 5
start_period: 5s
start_period: 10s

matching-service:
container_name: matching-service
Expand Down
98 changes: 98 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
- USER_SERVICE_URL=http://user-service:3002
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- COLLABORATION_SERVICE_URL=http://collaboration-service:3008
- RMQ_URL=http://rabbitmq:15672
- DOMAIN_NAME=${DOMAIN_NAME}
- VPS_SSL_CERT_FILE=${VPS_SSL_CERT_FILE}
Expand All @@ -31,6 +32,101 @@ services:
- matching-service
command: sh -c "envsubst < /etc/nginx/templates/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"

collaboration-service:
container_name: collaboration-service
image: glemenneo/cs3219-ay2425s1-project-g31-collaboration-service:latest
environment:
- NODE_ENV=development
- PORT=3008
- DB_URL=mongodb://${MONGO_USER}:${MONGO_PASSWORD}@collab-db:27017/collaboration-service?authSource=admin
- ACCESS_TOKEN_PUBLIC_KEY=${ACCESS_TOKEN_PUBLIC_KEY}
- ACCESS_TOKEN_PRIVATE_KEY=${ACCESS_TOKEN_PRIVATE_KEY}
- USER_SERVICE_URL=http://user-service:3002
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- JUDGE_ZERO_URL=http://judgezero-server:2358
- JUDGE_ZERO_SUBMIT_CONFIG=${JUDGE_ZERO_SUBMIT_CONFIG}
restart: always
networks:
- backend-network
depends_on:
- judgezero-server
- judgezero-workers
- collab-db

collab-db:
container_name: collab-db
image: mongo:8.0.0
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
ports:
- '27087:27017'
volumes:
- 'collab_data:/data/db'
networks:
- backend-network
command: mongod --quiet --logpath /dev/null --auth

judgezero-server:
image: judge0/judge0:1.13.1
volumes:
- ./judge0.conf:/judge0.conf:ro
networks:
- backend-network
privileged: true
restart: always
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy

judgezero-workers:
image: judge0/judge0:1.13.1
command: ["./scripts/workers"]
volumes:
- ./judge0.conf:/judge0.conf:ro
networks:
- backend-network
privileged: true
restart: always
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy

db:
image: postgres:16.2
env_file: judge0.conf
volumes:
- judgezero_data:/var/lib/postgresql/data/
networks:
- backend-network
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -U judge0"]
interval: 10s
retries: 5
start_period: 10s

redis:
image: redis:7.2.4
command: [
"bash", "-c",
'docker-entrypoint.sh --appendonly no --requirepass "$$REDIS_PASSWORD"'
]
env_file: judge0.conf
networks:
- backend-network
restart: always
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
retries: 5
start_period: 10s

matching-service:
container_name: matching-service
image: glemenneo/cs3219-ay2425s1-project-g31-matching-service:latest
Expand Down Expand Up @@ -145,6 +241,8 @@ volumes:
matching_data:
rabbitmq_data:
rabbitmq_log:
collab_data:
judgezero_data:

# Define a network, which allows containers to communicate
# with each other, by using their container name as a hostname
Expand Down
4 changes: 4 additions & 0 deletions nginx/templates/local-nginx.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ http {
proxy_pass ${RMQ_URL}/;
}

location /collab {
proxy_pass ${COLLABORATION_SERVICE_URL};
}

error_page 404 /404.json;
location /404.json {
return 404 '{"error":{"code":404,"message":"Not Found"}}';
Expand Down
Loading

0 comments on commit 58fc6eb

Please sign in to comment.