File size: 1,249 Bytes
ca28016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 });
  }
}