fix: read API_BASE_URL at request time in proxy route
The module-level const was being inlined at build time by Next.js standalone bundler, causing the proxy to always use localhost:8000 instead of the Docker runtime env var api-gateway:8000. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d8cb2a9c6f
commit
74e4c55277
|
|
@ -1,6 +1,9 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8000';
|
/** Read at request time so Docker runtime env vars are picked up (not inlined at build time). */
|
||||||
|
function getApiBaseUrl(): string {
|
||||||
|
return process.env.API_BASE_URL || 'http://localhost:8000';
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest, { params }: { params: { path: string[] } }) {
|
export async function GET(request: NextRequest, { params }: { params: { path: string[] } }) {
|
||||||
return proxyRequest(request, params.path);
|
return proxyRequest(request, params.path);
|
||||||
|
|
@ -19,7 +22,8 @@ export async function DELETE(request: NextRequest, { params }: { params: { path:
|
||||||
}
|
}
|
||||||
|
|
||||||
async function proxyRequest(request: NextRequest, path: string[]) {
|
async function proxyRequest(request: NextRequest, path: string[]) {
|
||||||
const url = `${API_BASE_URL}/${path.join('/')}${request.nextUrl.search}`;
|
const apiBase = getApiBaseUrl();
|
||||||
|
const url = `${apiBase}/${path.join('/')}${request.nextUrl.search}`;
|
||||||
|
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
request.headers.forEach((value, key) => {
|
request.headers.forEach((value, key) => {
|
||||||
|
|
@ -43,6 +47,7 @@ async function proxyRequest(request: NextRequest, path: string[]) {
|
||||||
headers: { 'Content-Type': response.headers.get('Content-Type') || 'application/json' },
|
headers: { 'Content-Type': response.headers.get('Content-Type') || 'application/json' },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return NextResponse.json({ error: 'Proxy error' }, { status: 502 });
|
console.error(`Failed to proxy ${url}`, error);
|
||||||
|
return NextResponse.json({ error: 'Proxy error', detail: String(error) }, { status: 502 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue