import { NextRequest, NextResponse } from 'next/server'; function resolveBackendBase(req: NextRequest): string { const host = req.headers.get('host') || ''; const url = new URL(req.url); if (host.startsWith('localhost') || url.hostname === 'localhost' || url.hostname === '127.0.0.1') { return 'http://localhost:5000'; } return process.env.NEXT_PUBLIC_GOLEM_SERVER_URL || 'http://localhost:5000'; } export async function OPTIONS() { return NextResponse.json({ ok: true }); } export async function POST(req: NextRequest) { try { const backendBase = resolveBackendBase(req); const contentType = req.headers.get('content-type') || 'application/json'; const bodyText = await req.text(); const resp = await fetch(`${backendBase}/generate`, { method: 'POST', headers: { 'Content-Type': contentType }, body: bodyText, cache: 'no-store', }); const text = await resp.text(); const ct = resp.headers.get('content-type') || 'application/json'; return new NextResponse(text, { status: resp.status, headers: { 'content-type': ct }, }); } catch (e: any) { return NextResponse.json({ success: false, error: String(e) }, { status: 500 }); } }