Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#196] Integrate collaboration service into deployment #198

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/collaboration-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ connectToDatabase(config.DB_URL)

const server: Server = http.createServer(index)
const wss = new WebSocket.Server({ noServer: true })
new WebSocketConnection(3009)
new WebSocketConnection(Number(config.WS_PORT))

yUtils.setPersistence({
bindState: async (docName, ydoc) => {
Expand Down
9 changes: 8 additions & 1 deletion backend/collaboration-service/src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class Config {
@IsNotEmpty()
PORT: string

@IsNumberString()
@IsNotEmpty()
WS_PORT: string

@IsString()
@IsNotEmpty()
DB_URL: string
Expand All @@ -30,6 +34,7 @@ export class Config {
constructor(
NODE_ENV: string,
PORT: string,
WS_PORT: string,
DB_URL: string,
ACCESS_TOKEN_PUBLIC_KEY: string,
USER_SERVICE_URL: string,
Expand All @@ -38,7 +43,8 @@ export class Config {
JUDGE_ZERO_URL: string
) {
this.NODE_ENV = NODE_ENV ?? 'development'
this.PORT = PORT ?? '3006'
this.WS_PORT = WS_PORT ?? '3009'
this.PORT = PORT ?? '3008'
this.DB_URL = DB_URL
this.ACCESS_TOKEN_PUBLIC_KEY = ACCESS_TOKEN_PUBLIC_KEY
this.USER_SERVICE_URL = USER_SERVICE_URL
Expand All @@ -51,6 +57,7 @@ export class Config {
return new Config(
env.NODE_ENV!,
env.PORT!,
env.WS_PORT!,
env.DB_URL!,
env.ACCESS_TOKEN_PUBLIC_KEY!,
env.USER_SERVICE_URL!,
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- COLLABORATION_SERVICE_URL=http://collaboration-service:3008
- COLLABORATION_SERVICE_CHAT_URL=http://collaboration-service:3009
- RMQ_URL=http://rabbitmq:15672
- DOMAIN_NAME=${DOMAIN_NAME}
image: nginx:1.27.2-alpine
Expand All @@ -31,6 +32,7 @@ services:
environment:
- NODE_ENV=development
- PORT=3008
- WS_PORT=3009
- 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}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- QUESTION_SERVICE_URL=http://question-service:3004
- MATCHING_SERVICE_URL=http://matching-service:3006
- COLLABORATION_SERVICE_URL=http://collaboration-service:3008
- COLLABORATION_SERVICE_CHAT_URL=http://collaboration-service:3009
- RMQ_URL=http://rabbitmq:15672
- DOMAIN_NAME=${DOMAIN_NAME}
- VPS_SSL_CERT_FILE=${VPS_SSL_CERT_FILE}
Expand Down Expand Up @@ -38,6 +39,7 @@ services:
environment:
- NODE_ENV=development
- PORT=3008
- WS_PORT=3009
- 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}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/dashboard/new-session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const NewSession = () => {
}

const socket = new WebSocket(
`${process.env.NEXT_PUBLIC_API_URL ?? 'ws://localhost:3006'}/matching/ws/?id=${websocketId}`
process.env.NEXT_PUBLIC_API_URL?.concat(`/matching/ws/?id=${websocketId}`) ?? 'ws://localhost:3006'
)
socketRef.current = socket
socketRef.current.onclose = () => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/pages/code/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default function Code() {
}, [])

useEffect(() => {
socketRef.current = io('ws://localhost:3009', {
socketRef.current = io(process.env.NEXT_PUBLIC_API_URL ?? 'ws://localhost:3009', {
path: '/collab/chat/ws/socket.io/',
auth: {
token: sessionData?.user.accessToken,
name: sessionData?.user.username,
Expand Down
11 changes: 8 additions & 3 deletions frontend/pages/code/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ const CodeMirrorEditor = forwardRef(({ roomId, language }: { roomId: string; lan
if (!session) return
const token = session.user.accessToken
if (!token) return undefined
const wsProvider = new WebsocketProvider('ws://localhost:3008', roomId, ydoc, {
protocols: [token],
})
const wsProvider = new WebsocketProvider(
process.env.NEXT_PUBLIC_API_URL?.concat('/collab/y/ws') ?? 'ws://localhost:3008',
roomId,
ydoc,
{
protocols: [token],
}
)
wsProvider.awareness.setLocalStateField('user', {
name: session.user.username || 'Anonymous',
color: userColor.color,
Expand Down
98 changes: 53 additions & 45 deletions nginx/templates/local-nginx.conf.template
Original file line number Diff line number Diff line change
@@ -1,64 +1,72 @@
events {}

http {
limit_req_zone ${D}binary_remote_addr zone=rate_limit_zone:10m rate=10r/s;
limit_req_status 429;
limit_req_zone ${D}binary_remote_addr zone=rate_limit_zone:10m rate=10r/s;
limit_req_status 429;

server {
listen 8080;
listen [::]:8080;
server {
listen 8080;
listen [::]:8080;

server_name ${DOMAIN_NAME};
server_name ${DOMAIN_NAME};

limit_req zone=rate_limit_zone burst=20 nodelay;
limit_req zone=rate_limit_zone burst=20 nodelay;

proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";

location /auth {
proxy_pass ${USER_SERVICE_URL};
}
location /auth {
proxy_pass ${USER_SERVICE_URL};
}

location /users {
proxy_pass ${USER_SERVICE_URL};
}
location /users {
proxy_pass ${USER_SERVICE_URL};
}

location /questions {
proxy_pass ${QUESTION_SERVICE_URL};
}
location /questions {
proxy_pass ${QUESTION_SERVICE_URL};
}

location /matching {
proxy_pass ${MATCHING_SERVICE_URL};
}
location /matching {
proxy_pass ${MATCHING_SERVICE_URL};
}

location /matching/ws/ {
proxy_pass ${MATCHING_SERVICE_URL}/;
}
location /matching/ws/ {
proxy_pass ${MATCHING_SERVICE_URL}/;
}

location /rabbitmq/ {
proxy_pass ${RMQ_URL}/;
}
location /rabbitmq/ {
proxy_pass ${RMQ_URL}/;
}

location /collab {
proxy_pass ${COLLABORATION_SERVICE_URL};
}
location /collab {
proxy_pass ${COLLABORATION_SERVICE_URL};
}

error_page 404 /404.json;
location /404.json {
return 404 '{"error":{"code":404,"message":"Not Found"}}';
}
location /collab/y/ws/ {
proxy_pass ${COLLABORATION_SERVICE_URL}/;
}

error_page 429 /429.json;
location /429.json {
return 429 '{"error":{"code":429,"message":"Too Many Requests"}}';
}
location /collab/chat/ws/ {
proxy_pass ${COLLABORATION_SERVICE_CHAT_URL}/;
}

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

error_page 429 /429.json;
location /429.json {
return 429 '{"error":{"code":429,"message":"Too Many Requests"}}';
}

error_page 502 /502.json;
location /502.json {
return 502 '{"error":{"code":502,"message":"Bad Gateway"}}';
error_page 502 /502.json;
location /502.json {
return 502 '{"error":{"code":502,"message":"Bad Gateway"}}';
}
}
}
}
132 changes: 70 additions & 62 deletions nginx/templates/nginx.conf.template
Original file line number Diff line number Diff line change
@@ -1,87 +1,95 @@
events {}

http {
limit_req_zone ${D}binary_remote_addr zone=rate_limit_zone:10m rate=10r/s;
limit_req_status 429;
limit_req_zone ${D}binary_remote_addr zone=rate_limit_zone:10m rate=10r/s;
limit_req_status 429;

server {
listen 80 default_server;
listen [::]:80 default_server;
server {
listen 80 default_server;
listen [::]:80 default_server;

server_name ${DOMAIN_NAME};
server_name ${DOMAIN_NAME};

limit_req zone=rate_limit_zone burst=20 nodelay;
limit_req zone=rate_limit_zone burst=20 nodelay;

location /api/ {
proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass https://127.0.0.1:443/;
}
location /api/ {
proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass https://127.0.0.1:443/;
}
}

server {
listen 443 ssl;
listen [::]:443 ssl;
server {
listen 443 ssl;
listen [::]:443 ssl;

server_name ${DOMAIN_NAME};
server_name ${DOMAIN_NAME};

ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
include /etc/nginx/ssl/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/ssl/server-dhparams.pem;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
include /etc/nginx/ssl/options-ssl-nginx.conf;
ssl_dhparam /etc/nginx/ssl/server-dhparams.pem;

limit_req zone=rate_limit_zone burst=20 nodelay;
limit_req zone=rate_limit_zone burst=20 nodelay;

proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For ${D}proxy_add_x_forwarded_for;
proxy_set_header Host ${D}host;
proxy_http_version 1.1;
proxy_set_header Upgrade ${D}http_upgrade;
proxy_set_header Connection "upgrade";

location /auth {
proxy_pass ${USER_SERVICE_URL};
}
location /auth {
proxy_pass ${USER_SERVICE_URL};
}

location /users {
proxy_pass ${USER_SERVICE_URL};
}
location /users {
proxy_pass ${USER_SERVICE_URL};
}

location /questions {
proxy_pass ${QUESTION_SERVICE_URL};
}
location /questions {
proxy_pass ${QUESTION_SERVICE_URL};
}

location /matching {
proxy_pass ${MATCHING_SERVICE_URL};
}
location /matching {
proxy_pass ${MATCHING_SERVICE_URL};
}

location /matching/ws/ {
proxy_pass ${MATCHING_SERVICE_URL}/;
}
location /matching/ws/ {
proxy_pass ${MATCHING_SERVICE_URL}/;
}

location /rabbitmq/ {
proxy_pass ${RMQ_URL}/;
}
location /rabbitmq/ {
proxy_pass ${RMQ_URL}/;
}

location /collab {
proxy_pass ${COLLABORATION_SERVICE_URL};
}
location /collab {
proxy_pass ${COLLABORATION_SERVICE_URL};
}

error_page 404 /404.json;
location /404.json {
return 404 '{"error":{"code":404,"message":"Not Found"}}';
}
location /collab/y/ws/ {
proxy_pass ${COLLABORATION_SERVICE_URL}/;
}

error_page 429 /429.json;
location /429.json {
return 429 '{"error":{"code":429,"message":"Too Many Requests"}}';
}
location /collab/chat/ws/ {
proxy_pass ${COLLABORATION_SERVICE_CHAT_URL}/;
}

error_page 502 /502.json;
location /502.json {
return 502 '{"error":{"code":502,"message":"Bad Gateway"}}';
error_page 404 /404.json;
location /404.json {
return 404 '{"error":{"code":404,"message":"Not Found"}}';
}

error_page 429 /429.json;
location /429.json {
return 429 '{"error":{"code":429,"message":"Too Many Requests"}}';
}

error_page 502 /502.json;
location /502.json {
return 502 '{"error":{"code":502,"message":"Bad Gateway"}}';
}
}
}
}
Loading