|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | + |
| 3 | +const BACKEND_URL = process.env.BACKEND_URL || process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8000' |
| 4 | +const API_KEY = process.env.BACKEND_API_KEY || '' |
| 5 | + |
| 6 | +/** |
| 7 | + * Catch-all proxy route that forwards requests to the backend with the API key. |
| 8 | + * Browser calls /api/backend/... → this route adds X-API-Key → forwards to backend. |
| 9 | + */ |
| 10 | +async function proxyRequest( |
| 11 | + request: NextRequest, |
| 12 | + { params }: { params: Promise<{ path: string[] }> } |
| 13 | +) { |
| 14 | + const { path } = await params |
| 15 | + const backendPath = '/' + path.join('/') |
| 16 | + const searchParams = request.nextUrl.searchParams.toString() |
| 17 | + const url = `${BACKEND_URL}${backendPath}${searchParams ? `?${searchParams}` : ''}` |
| 18 | + |
| 19 | + const headers = new Headers() |
| 20 | + // Forward relevant headers from the original request |
| 21 | + const contentType = request.headers.get('content-type') |
| 22 | + if (contentType) { |
| 23 | + headers.set('Content-Type', contentType) |
| 24 | + } |
| 25 | + const accept = request.headers.get('accept') |
| 26 | + if (accept) { |
| 27 | + headers.set('Accept', accept) |
| 28 | + } |
| 29 | + |
| 30 | + // Add API key (server-side only — never exposed to browser) |
| 31 | + if (API_KEY) { |
| 32 | + headers.set('X-API-Key', API_KEY) |
| 33 | + } |
| 34 | + |
| 35 | + const fetchOptions: RequestInit = { |
| 36 | + method: request.method, |
| 37 | + headers, |
| 38 | + } |
| 39 | + |
| 40 | + // Forward body for methods that have one |
| 41 | + if (request.method !== 'GET' && request.method !== 'HEAD') { |
| 42 | + // Check if it's a form/multipart upload |
| 43 | + if (contentType?.includes('multipart/form-data')) { |
| 44 | + fetchOptions.body = await request.arrayBuffer() |
| 45 | + // Let fetch set the correct content-type with boundary |
| 46 | + headers.delete('Content-Type') |
| 47 | + headers.set('Content-Type', contentType) |
| 48 | + } else { |
| 49 | + fetchOptions.body = await request.arrayBuffer() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + const response = await fetch(url, fetchOptions) |
| 55 | + |
| 56 | + // Stream the response back |
| 57 | + const responseHeaders = new Headers() |
| 58 | + response.headers.forEach((value, key) => { |
| 59 | + // Skip headers that Next.js manages |
| 60 | + if (!['transfer-encoding', 'connection'].includes(key.toLowerCase())) { |
| 61 | + responseHeaders.set(key, value) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + return new NextResponse(response.body, { |
| 66 | + status: response.status, |
| 67 | + statusText: response.statusText, |
| 68 | + headers: responseHeaders, |
| 69 | + }) |
| 70 | + } catch (error) { |
| 71 | + console.error('Backend proxy error:', error) |
| 72 | + return NextResponse.json( |
| 73 | + { detail: 'Failed to reach backend service' }, |
| 74 | + { status: 502 } |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export const GET = proxyRequest |
| 80 | +export const POST = proxyRequest |
| 81 | +export const PUT = proxyRequest |
| 82 | +export const PATCH = proxyRequest |
| 83 | +export const DELETE = proxyRequest |
0 commit comments