Skip to content

Commit f78600f

Browse files
committed
Add API gateway support for websockets
1 parent 3fc52d7 commit f78600f

File tree

10 files changed

+73
-27
lines changed

10 files changed

+73
-27
lines changed

api_gateway/src/index.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
import express from 'express';
22
import cookieParser from 'cookie-parser';
33
import { setupProxies } from './proxy/proxy';
4-
import { routes_config } from './proxy/routes_config';
4+
import { routes_config, ws_collab_proxy_config, ws_match_proxy_config } from './proxy/routes_config';
55
import { jwtCheck, onCredentialFailure } from './middleware/token_check';
66
import { setupLogging } from './middleware/logging';
77
import { isLocal } from './proxy/service_addresses';
8+
import { createProxyMiddleware } from 'http-proxy-middleware';
89

9-
const app = express();
10-
const port = process.env.PORT || 8000;
10+
const httpApp = express();
11+
const httpProxyPort: number = parseInt(process.env.PORT || "8000");
1112

12-
app.use(cookieParser())
13+
const wsMatchMakeApp = express();
14+
const wsMatchMakePort : number = parseInt(process.env.PORT || "7999");
1315

14-
setupLogging(app);
15-
app.use(jwtCheck.unless({ path: [/\/auth\//] }));
16-
app.use(onCredentialFailure);
16+
const wsCollabApp = express();
17+
const wsCollabAppPort : number = parseInt(process.env.PORT || "7998");
1718

18-
setupProxies(app, routes_config);
1919

20-
app.listen(port, () => {
20+
httpApp.use(cookieParser())
21+
22+
setupLogging(httpApp);
23+
httpApp.use(jwtCheck.unless({ path: [/\/auth\//] }));
24+
httpApp.use(onCredentialFailure);
25+
26+
setupProxies(httpApp, routes_config);
27+
setupProxies(wsMatchMakeApp, ws_match_proxy_config)
28+
setupProxies(wsCollabApp, ws_collab_proxy_config)
29+
30+
31+
32+
33+
httpApp.listen(httpProxyPort, () => {
34+
console.log(`API Gateway running in ${isLocal ? "local" : "docker"} mode`)
35+
console.log(`API Gateway listening at http://localhost:${httpProxyPort}`);
36+
})
37+
38+
wsMatchMakeApp.listen(wsMatchMakePort, () => {
39+
console.log(`API Gateway running in ${isLocal ? "local" : "docker"} mode`)
40+
console.log(`API Gateway listening at http://localhost:${wsMatchMakePort}`);
41+
})
42+
43+
wsCollabApp.listen(wsCollabAppPort, () => {
2144
console.log(`API Gateway running in ${isLocal ? "local" : "docker"} mode`)
22-
console.log(`API Gateway listening at http://localhost:${port}`);
45+
console.log(`API Gateway listening at http://localhost:${wsCollabAppPort}`);
2346
})

api_gateway/src/proxy/routes_config.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { matchServiceHostUrl, questionServiceUrl, userServiceHostUrl } from "./service_addresses";
1+
import { collabServiceHostUrl, matchServiceHostUrl, questionServiceUrl, userServiceHostUrl } from "./service_addresses";
22

33
export const routes_config = [
44
{
@@ -24,13 +24,26 @@ export const routes_config = [
2424
'/auth': '/' }
2525
}
2626
},
27+
]
28+
29+
export const ws_match_proxy_config = [
2730
{
28-
url: "/matchmake",
31+
url: "/",
2932
proxy: {
3033
target: matchServiceHostUrl,
3134
ws: true,
32-
pathRewrite: {
33-
'/matchmake': '/' }
35+
changeOrigin: true,
36+
}
37+
}
38+
]
39+
40+
export const ws_collab_proxy_config = [
41+
{
42+
url: "/",
43+
proxy: {
44+
target: collabServiceHostUrl,
45+
ws: true,
46+
changeOrigin: true,
3447
}
3548
}
3649
]

api_gateway/src/proxy/service_addresses.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export const isLocal = process.env.ENV_TYPE !== "docker";
33

44
export const questionServiceUrl = isLocal ? "http://localhost:8080" : "http://question-service:8080/";
55
export const userServiceHostUrl = isLocal ? "http://localhost:8081" : "http://user-service:8081/";
6-
export const matchServiceHostUrl = isLocal ? "ws://localhost:8082" : "ws://matching-service:8082"
6+
export const matchServiceHostUrl = isLocal ? "http://localhost:8082" : "http://matching-service:8082"
7+
export const collabServiceHostUrl = isLocal ? "http://localhost:8083" : "http://collab-service:8083"

docker_compose_dev.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ services:
99
ENV_TYPE: docker
1010
ports:
1111
- "8000:8000"
12+
- "7999:7999"
13+
- "7998:7998"
1214
depends_on:
1315
- question-service
1416
- user-service
17+
- matching-service
18+
- collab-service
1519

1620
question-service:
1721
container_name: question-service

frontend/src/api/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiGatewayClient } from "./server"
1+
import { apiGatewayClient } from "./gateway"
22

33
/**
44
* Makes a login request and sets the access_token if successful

frontend/src/api/server.ts renamed to frontend/src/api/gateway.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ export const apiGatewayClient = axios.create({
44
baseURL:'http://localhost:8000',
55
withCredentials: true
66
})
7+
8+
export const wsMatchMakeURL = 'http://localhost:7999'
9+
export const wsCollabUrl = 'ws://localhost:7998'

frontend/src/api/questions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AxiosResponse } from "axios";
22
import { Question } from "../models/Question.model";
3-
import { questionServiceClient } from "./server";
3+
import { apiGatewayClient } from "./gateway";
44

55
export async function fetchAllQuestions() {
6-
const response = await questionServiceClient.get('/api/questions');
6+
const response = await apiGatewayClient.get('/api/questions');
77

88
const resData = response.data;
99
const questions : Question[] = resData.map((q : any) => new Question(q._id, q.id, q.title, q.description, q.topics, q.difficulty))
@@ -18,7 +18,7 @@ export async function fetchAllQuestions() {
1818
*/
1919
export async function fetchQuestion(_id: string) {
2020
try {
21-
const response = await questionServiceClient.get(`/api/questions/${_id}`);
21+
const response = await apiGatewayClient.get(`/api/questions/${_id}`);
2222

2323
const resData = response.data;
2424
const resQuestion : Question = new Question(
@@ -43,7 +43,7 @@ export async function fetchQuestion(_id: string) {
4343
*/
4444
export async function addQuestion(question : Question) {
4545

46-
const response : AxiosResponse = await questionServiceClient.post('/api/questions', {
46+
const response : AxiosResponse = await apiGatewayClient.post('/api/questions', {
4747
title: question.title,
4848
description: question.descMd,
4949
topics: question.topics,
@@ -67,7 +67,7 @@ export async function addQuestion(question : Question) {
6767
* @param _id The uuid identifying the question resource
6868
*/
6969
export async function delQuestion(_id : string) {
70-
const response = await questionServiceClient.delete(`/api/questions/${_id}`);
70+
const response = await apiGatewayClient.delete(`/api/questions/${_id}`);
7171

7272
return response;
7373
}
@@ -81,7 +81,7 @@ export async function delQuestion(_id : string) {
8181
* @returns
8282
*/
8383
export async function updateQuestion(_id: string, question: Question) {
84-
const response : AxiosResponse = await questionServiceClient.put(`/api/questions/${_id}`, {
84+
const response : AxiosResponse = await apiGatewayClient.put(`/api/questions/${_id}`, {
8585
title: question.title,
8686
description: question.descMd,
8787
topics: question.topics,

frontend/src/api/user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxiosResponse } from "axios";
22
import { SolvedQuestion } from "../models/SolvedQuestion.model"; // Import the SolvedQuestion model
3-
import { userServiceClient } from "./server";
3+
import { apiGatewayClient } from "./gateway";
44

55
/**
66
* Fetch questions completed by the user.
@@ -11,7 +11,7 @@ export async function fetchUserCompletedQuestions(
1111
userId: String
1212
): Promise<SolvedQuestion[]> {
1313
try {
14-
const response: AxiosResponse = await userServiceClient.get(
14+
const response: AxiosResponse = await apiGatewayClient.get(
1515
`/api/users/${userId}/questions`
1616
);
1717

@@ -53,7 +53,7 @@ export async function addUserQuestion(
5353
category: string[]
5454
): Promise<SolvedQuestion> {
5555
try {
56-
const response: AxiosResponse = await userServiceClient.post(
56+
const response: AxiosResponse = await apiGatewayClient.post(
5757
`/api/users/${userId}/addquestions`,
5858
{
5959
userId,

frontend/src/contexts/matchmake.context.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { selectUser } from "../reducers/authSlice";
1010
import { useSelector } from "react-redux";
1111
import { useNavigate } from "react-router-dom";
1212
import { useToast } from "@chakra-ui/react";
13+
import { wsMatchMakeURL } from "../api/gateway";
1314

1415
interface RoomDetail {
1516
partner: string;
@@ -45,7 +46,7 @@ export const MatchmakeProvider = ({
4546
const toast = useToast();
4647

4748
useEffect(() => {
48-
const socket = io("ws://localhost:8082", {
49+
const socket = io(wsMatchMakeURL, {
4950
autoConnect: false,
5051
});
5152
setSocket(socket);

frontend/src/contexts/sharededitor.context.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useLoaderData } from "react-router-dom";
1616
import { Buffer } from "buffer";
1717
import data from "../data/lang_temps.json";
1818
import { ToastId, useToast } from "@chakra-ui/react";
19+
import { wsCollabUrl } from "../api/gateway";
1920

2021
export type language = keyof typeof data;
2122

@@ -295,7 +296,7 @@ export const SharedEditorProvider = ({
295296
"base64"
296297
);
297298
const provider = new WebrtcProvider(roomvalue, doc, {
298-
signaling: ["ws://localhost:8083"],
299+
signaling: [wsCollabUrl],
299300
filterBcConns: true,
300301
});
301302
setProvider(provider);

0 commit comments

Comments
 (0)