From 2836d2d0943bbd0fb4d068ca361e269ea2c7df52 Mon Sep 17 00:00:00 2001 From: Glemen Neo Date: Sat, 9 Nov 2024 18:46:48 +0800 Subject: [PATCH 1/2] [#206] feat: add api suffix for all public api calls --- frontend/components/dashboard/new-session.tsx | 2 +- frontend/pages/api/auth/[...nextauth].ts | 2 +- frontend/pages/code/editor.tsx | 2 +- frontend/services/axios-middleware.ts | 10 ++++++---- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/frontend/components/dashboard/new-session.tsx b/frontend/components/dashboard/new-session.tsx index 515c07d052..15b8b6f580 100644 --- a/frontend/components/dashboard/new-session.tsx +++ b/frontend/components/dashboard/new-session.tsx @@ -82,7 +82,7 @@ export const NewSession = () => { // Refactor this const wsUrl = (process.env.NEXT_PUBLIC_API_URL || 'ws://localhost:3006')?.concat( - `/matching/ws/?id=${websocketId}` + `/api/matching/ws/?id=${websocketId}` ) const socket = new WebSocket(wsUrl) setTimeout(() => { diff --git a/frontend/pages/api/auth/[...nextauth].ts b/frontend/pages/api/auth/[...nextauth].ts index c46d6be6b8..ccae3f1646 100644 --- a/frontend/pages/api/auth/[...nextauth].ts +++ b/frontend/pages/api/auth/[...nextauth].ts @@ -24,7 +24,7 @@ export default NextAuth({ try { const api = axios.create({ - baseURL: process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3002', + baseURL: process.env.NEXT_PUBLIC_API_URL?.concat('/api') ?? 'http://localhost:3002', }) const response = await api.post('/auth/login', { usernameOrEmail: username, password }) diff --git a/frontend/pages/code/editor.tsx b/frontend/pages/code/editor.tsx index f57cd5f51d..7e73465ffa 100644 --- a/frontend/pages/code/editor.tsx +++ b/frontend/pages/code/editor.tsx @@ -52,7 +52,7 @@ const CodeMirrorEditor = forwardRef(({ roomId, language }: IProps, ref) => { const token = session.user.accessToken if (!token) return undefined const wsProvider = new WebsocketProvider( - process.env.NEXT_PUBLIC_API_URL?.concat('/collab/y/ws') ?? 'ws://localhost:3008', + process.env.NEXT_PUBLIC_API_URL?.concat('/api/collab/y/ws') ?? 'ws://localhost:3008', roomId, ydoc, { diff --git a/frontend/services/axios-middleware.ts b/frontend/services/axios-middleware.ts index 7894b7d640..02d93b693d 100644 --- a/frontend/services/axios-middleware.ts +++ b/frontend/services/axios-middleware.ts @@ -38,9 +38,11 @@ const createServiceAPI = (baseURL: string) => { return api } -const userServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3002') -const questionServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3004') -const matchingServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3006') -const collaborationServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3008') +const userServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL?.concat('/api') ?? 'http://localhost:3002') +const questionServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL?.concat('/api') ?? 'http://localhost:3004') +const matchingServiceAPI = createServiceAPI(process.env.NEXT_PUBLIC_API_URL?.concat('/api') ?? 'http://localhost:3006') +const collaborationServiceAPI = createServiceAPI( + process.env.NEXT_PUBLIC_API_URL?.concat('/api') ?? 'http://localhost:3008' +) export default { userServiceAPI, questionServiceAPI, matchingServiceAPI, collaborationServiceAPI } From 600de35e3a7976f503e37f1e170d7e3eda5c1624 Mon Sep 17 00:00:00 2001 From: Glemen Neo Date: Sun, 10 Nov 2024 00:28:42 +0800 Subject: [PATCH 2/2] [#206] feat: add rewrite rule to strip /api prefixes --- nginx/templates/local-nginx.conf.template | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nginx/templates/local-nginx.conf.template b/nginx/templates/local-nginx.conf.template index ba0a8511ad..a6122d34be 100644 --- a/nginx/templates/local-nginx.conf.template +++ b/nginx/templates/local-nginx.conf.template @@ -18,6 +18,10 @@ http { proxy_set_header Upgrade ${D}http_upgrade; proxy_set_header Connection "upgrade"; + location /api/ { + rewrite ^/api(/.*)$ $1 last; + } + location /auth { proxy_pass ${USER_SERVICE_URL}; }