Skip to content

Commit

Permalink
Revert API routes to use dynamic base URL
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zacharyLYH committed Dec 13, 2023
1 parent d2b3b7e commit 03ea102
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
8 changes: 5 additions & 3 deletions app/api/code/score/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions app/api/code/submit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 03ea102

Please sign in to comment.