fix: replace NEXT_PUBLIC_API_URL with Next.js Route Handler proxy for runtime backend URL
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> Agent-Logs-Url: https://github.com/christianlouis/pop_puller_to_gmail/sessions/59e2f604-9ad3-46ef-8670-fd6b3b7c14b2
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const BACKEND_URL = process.env.BACKEND_URL ?? "http://localhost:8000";
|
||||
|
||||
type RouteContext = { params: Promise<{ path: string[] }> };
|
||||
|
||||
async function handler(request: NextRequest, context: RouteContext) {
|
||||
const { path } = await context.params;
|
||||
const targetUrl = `${BACKEND_URL}/api/v1/${path.join("/")}${request.nextUrl.search}`;
|
||||
|
||||
const headers = new Headers(request.headers);
|
||||
headers.delete("host");
|
||||
// Request uncompressed response so the body can be forwarded as-is
|
||||
headers.set("accept-encoding", "identity");
|
||||
|
||||
const hasBody = request.method !== "GET" && request.method !== "HEAD";
|
||||
const body = hasBody ? await request.arrayBuffer() : undefined;
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
method: request.method,
|
||||
headers,
|
||||
body,
|
||||
});
|
||||
|
||||
return new NextResponse(upstream.body, {
|
||||
status: upstream.status,
|
||||
statusText: upstream.statusText,
|
||||
headers: upstream.headers,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Proxy error forwarding to backend:", error);
|
||||
return NextResponse.json({ detail: "Backend unavailable" }, { status: 502 });
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = handler;
|
||||
export const POST = handler;
|
||||
export const PUT = handler;
|
||||
export const PATCH = handler;
|
||||
export const DELETE = handler;
|
||||
export const HEAD = handler;
|
||||
export const OPTIONS = handler;
|
||||
@@ -1,10 +1,7 @@
|
||||
import axios from "axios";
|
||||
|
||||
const API_BASE_URL =
|
||||
process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: `${API_BASE_URL}/api/v1`,
|
||||
baseURL: `/api/v1`,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user