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:
hailin 2026-02-21 22:45:57 -08:00
parent d8cb2a9c6f
commit 74e4c55277
1 changed files with 8 additions and 3 deletions

View File

@ -1,6 +1,9 @@
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[] } }) {
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[]) {
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> = {};
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' },
});
} 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 });
}
}