Skip to content

Commit 9218a30

Browse files
committed
Add support for web sockets to API gateway
1 parent 3fc4f2c commit 9218a30

File tree

8 files changed

+71
-16
lines changed

8 files changed

+71
-16
lines changed

api-gateway/nginx.conf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
user nginx;
2+
worker_processes auto;
3+
4+
error_log /var/log/nginx/error.log notice;
5+
pid /var/run/nginx.pid;
6+
7+
8+
events {
9+
worker_connections 1024;
10+
}
11+
12+
13+
http {
14+
include /etc/nginx/mime.types;
15+
default_type application/octet-stream;
16+
17+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
18+
'$status $body_bytes_sent "$http_referer" '
19+
'"$http_user_agent" "$http_x_forwarded_for"';
20+
21+
access_log /var/log/nginx/access.log main;
22+
23+
sendfile on;
24+
#tcp_nopush on;
25+
26+
keepalive_timeout 65;
27+
28+
#gzip on;
29+
30+
map $http_upgrade $connection_upgrade {
31+
default upgrade;
32+
'' close;
33+
}
34+
35+
include /etc/nginx/conf.d/*.conf;
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
location /public/matching-service/ {
2+
location /public/matching-service/match/ {
3+
location /public/matching-service/match/subscribe/ {
4+
proxy_pass http://matching-service/match/subscribe/;
5+
proxy_http_version 1.1;
6+
proxy_set_header Upgrade $http_upgrade;
7+
proxy_set_header Connection $connection_upgrade;
8+
proxy_set_header Host $host;
9+
proxy_set_header X-Real-IP $remote_addr;
10+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
11+
proxy_set_header X-Forwarded-Proto $scheme;
12+
}
13+
}
214
}

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ services:
4040
image: nginx:1.26
4141
volumes:
4242
- ./api-gateway/templates:/etc/nginx/templates
43+
- ./api-gateway/nginx.conf:/etc/nginx/nginx.conf
4344
ports:
4445
- $API_GATEWAY_PORT:$API_GATEWAY_PORT
4546
environment:

frontend/components/matching/find-match.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { SearchProgress } from "@/components/matching/search-progress";
55
import { SelectionSummary } from "@/components/matching/selection-summary";
66
import { useToast } from "@/components/hooks/use-toast";
77
import { useAuth } from "@/app/auth/auth-context";
8-
import { joinMatchQueue } from "@/lib/join-match-queue";
9-
import { leaveMatchQueue } from "@/lib/leave-match-queue";
10-
import { subscribeMatch } from "@/lib/subscribe-match";
8+
import { joinMatchQueue } from "@/lib/api/matching-service/join-match-queue";
9+
import { leaveMatchQueue } from "@/lib/api/matching-service/leave-match-queue";
10+
import { subscribeMatch } from "@/lib/api/matching-service/subscribe-match";
1111

1212
export default function FindMatch() {
1313
const [selectedDifficulty, setSelectedDifficulty] = useState<string>("");

frontend/lib/api/api-uri.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const constructUri = (
1313
`http://${process.env.NEXT_PUBLIC_BASE_URI || baseUri}:${process.env.NEXT_PUBLIC_API_GATEWAY_PORT}/${authType}/${serviceName}`;
1414

1515
export const userServiceUri: (baseUri: string, authType: AuthType) => string = (
16-
baseUri,
17-
authType
16+
baseUri: string,
17+
authType: AuthType
1818
) => constructUri(baseUri, authType, "user-service");
1919
export const questionServiceUri: (
2020
baseUri: string,
@@ -27,9 +27,15 @@ export const matchingServiceUri: (
2727
) => string = (baseUri, authType) =>
2828
constructUri(baseUri, authType, "matching-service");
2929

30-
const constructWebSockUri = (baseUri: string) =>
31-
`ws://${process.env.NEXT_PUBLIC_BASE_URI || baseUri}:${process.env.NEXT_PUBLIC_API_GATEWAY_PORT}`;
30+
const constructWebSockUri = (
31+
baseUri: string,
32+
authType: AuthType,
33+
serviceName: string
34+
) =>
35+
`ws://${process.env.NEXT_PUBLIC_BASE_URI || baseUri}:${process.env.NEXT_PUBLIC_API_GATEWAY_PORT}/${authType}/${serviceName}`;
3236

33-
export const matchingServiceWebSockUri: (baseUri: string) => string = (
34-
baseUri
35-
) => constructWebSockUri(baseUri);
37+
export const matchingServiceWebSockUri: (
38+
baseUri: string,
39+
authType: AuthType
40+
) => string = (baseUri, authType) =>
41+
constructWebSockUri(baseUri, authType, "matching-service");

frontend/lib/join-match-queue.ts renamed to frontend/lib/api/matching-service/join-match-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { matchingServiceUri } from "@/lib/api/api-uri";
1+
import { AuthType, matchingServiceUri } from "@/lib/api/api-uri";
22

33
export const joinMatchQueue = async (
44
jwtToken: string,
@@ -11,7 +11,7 @@ export const joinMatchQueue = async (
1111
difficulty: complexity,
1212
}).toString();
1313
const response = await fetch(
14-
`${matchingServiceUri(window.location.hostname)}/match/queue/${userId}?${params}`,
14+
`${matchingServiceUri(window.location.hostname, AuthType.Private)}/match/queue/${userId}?${params}`,
1515
{
1616
method: "POST",
1717
headers: {

frontend/lib/leave-match-queue.ts renamed to frontend/lib/api/matching-service/leave-match-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { matchingServiceUri } from "@/lib/api/api-uri";
1+
import { AuthType, matchingServiceUri } from "@/lib/api/api-uri";
22

33
export const leaveMatchQueue = async (
44
jwtToken: string,
@@ -11,7 +11,7 @@ export const leaveMatchQueue = async (
1111
difficulty: complexity,
1212
}).toString();
1313
const response = await fetch(
14-
`${matchingServiceUri(window.location.hostname)}/match/queue/${userId}?${params}`,
14+
`${matchingServiceUri(window.location.hostname, AuthType.Private)}/match/queue/${userId}?${params}`,
1515
{
1616
method: "DELETE",
1717
headers: {

frontend/lib/subscribe-match.ts renamed to frontend/lib/api/matching-service/subscribe-match.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { matchingServiceWebSockUri } from "@/lib/api/api-uri";
1+
import { AuthType, matchingServiceWebSockUri } from "@/lib/api/api-uri";
22

33
export const subscribeMatch = async (
44
userId: string,
@@ -10,6 +10,6 @@ export const subscribeMatch = async (
1010
difficulty: complexity,
1111
});
1212
return new WebSocket(
13-
`${matchingServiceWebSockUri(window.location.hostname)}/match/subscribe/${userId}?${params}`
13+
`${matchingServiceWebSockUri(window.location.hostname, AuthType.Public)}/match/subscribe/${userId}?${params}`
1414
);
1515
};

0 commit comments

Comments
 (0)