From 03ea102edb774a521092a7efeed7ad12ca8fb265 Mon Sep 17 00:00:00 2001 From: Zac Date: Wed, 13 Dec 2023 15:37:28 +0800 Subject: [PATCH] Revert API routes to use dynamic base URL This commit refactors the API routes to use a dynamic base URL extracted from the incoming request. This ensures that the correct base URL is used for Axios requests, improving the flexibility and reliability of the code. --- app/api/code/score/route.ts | 8 +++++--- app/api/code/submit/route.ts | 16 ++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/api/code/score/route.ts b/app/api/code/score/route.ts index 8375e90..1d3af9d 100644 --- a/app/api/code/score/route.ts +++ b/app/api/code/score/route.ts @@ -83,14 +83,16 @@ export async function POST(req: Request) { } else { score = "58"; } - + // Extract the host and protocol from the incoming request + const url = new URL(req.url); + const baseUrl = `${url.protocol}//${url.host}`; console.log( "ABOUT TO SEND RESULT ", score, "to", - "https://" + process.env.VERCEL_URL + `/api/feedback/result` + `${baseUrl}/api/feedback/result` ); - axios.post("https://" + process.env.VERCEL_URL + `/api/feedback/result`, { + axios.post(`${baseUrl}/api/feedback/result`, { score, challenge, code, diff --git a/app/api/code/submit/route.ts b/app/api/code/submit/route.ts index d9bae8a..8f4dc8c 100644 --- a/app/api/code/submit/route.ts +++ b/app/api/code/submit/route.ts @@ -76,18 +76,14 @@ export async function POST(req: Request) { lastSubmission: new Date().toISOString(), }); } - - console.log("BASE URL: ", "https://" + process.env.VERCEL_URL); + // Extract the host and protocol from the incoming request + const url = new URL(req.url); + const baseUrl = `${url.protocol}//${url.host}`; + console.log("BASE URL: ", baseUrl); // Use the base URL for Axios requests - axios.put( - "https://" + process.env.VERCEL_URL + `/api/increment/submit`, - {} - ); + axios.put(`${baseUrl}/api/increment/submit`, {}); - axios.post( - "https://" + process.env.VERCEL_URL + `/api/code/score`, - body - ); + axios.post(`${baseUrl}/api/code/score`, body); return NextResponse.json({ message: "Accepted" }, { status: 202 }); } catch (error) {