const BACKEND_BASE = process.env.BACKEND_INTERNAL_URL || process.env.NEXT_PUBLIC_BACKEND_URL;

export async function GET() {
  if (!BACKEND_BASE) {
    return new Response('NEXT_PUBLIC_BACKEND_URL 未配置', { status: 500 });
  }
  try {
  const resp = await fetch(`${BACKEND_BASE}/configs/data_sources`, {
    headers: { 'Content-Type': 'application/json' },
    cache: 'no-store',
  });
  const text = await resp.text();
  return new Response(text, {
    status: resp.status,
    headers: { 'Content-Type': resp.headers.get('Content-Type') || 'application/json' },
  });
  } catch (e: any) {
    const errorBody = JSON.stringify({ message: e?.message || '连接后端失败' });
    return new Response(errorBody, { status: 502, headers: { 'Content-Type': 'application/json' } });
  }
}

export async function PUT(req: Request) {
  if (!BACKEND_BASE) {
    return new Response('NEXT_PUBLIC_BACKEND_URL 未配置', { status: 500 });
  }
  const body = await req.text();
  try {
  const resp = await fetch(`${BACKEND_BASE}/configs/data_sources`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body,
  });
  const text = await resp.text();
  return new Response(text, {
    status: resp.status,
    headers: { 'Content-Type': resp.headers.get('Content-Type') || 'application/json' },
  });
  } catch (e: any) {
    const errorBody = JSON.stringify({ message: e?.message || '连接后端失败' });
    return new Response(errorBody, { status: 502, headers: { 'Content-Type': 'application/json' } });
  }
}


