runtime backend proxy

This commit is contained in:
2026-04-14 22:52:01 -06:00
parent d35c820967
commit dea1caafd6
10 changed files with 1868 additions and 11 deletions

View File

@@ -0,0 +1,25 @@
const BACKEND = process.env.BACKEND_URL || 'http://localhost:8080'
async function proxy(request, { params }) {
const path = (await params).path.join('/')
const { search } = new URL(request.url)
const url = `${BACKEND}/api/${path}${search}`
const headers = new Headers(request.headers)
headers.delete('host')
const init = { method: request.method, headers }
if (!['GET', 'HEAD'].includes(request.method)) {
init.body = request.body
init.duplex = 'half'
}
const res = await fetch(url, init)
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers: res.headers,
})
}
export { proxy as GET, proxy as POST, proxy as PUT, proxy as DELETE, proxy as PATCH }

View File

@@ -1,11 +0,0 @@
import { NextResponse } from 'next/server'
export function middleware(request) {
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8080'
const { pathname, search } = request.nextUrl
return NextResponse.rewrite(new URL(pathname + search, backendUrl))
}
export const config = {
matcher: '/api/:path*',
}