Spaces:
Sleeping
Sleeping
Caner Giden commited on
Refactor GET function in core-health route
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import { NextResponse } from "next/server";export const runtime = "nodejs";export const dynamic = "force-dynamic";function withTimeout(ms: number) {const controller = new AbortController();const id = setTimeout(() => controller.abort(), ms);return { controller, clear: () => clearTimeout(id) };}export async function GET() {const base =process.env.NEXT_PUBLIC_API_BASE ||process.env.NEXT_PUBLIC_API_BASE_URL ||process.env.CORE_API_BASE;if (!base) {return NextResponse.json({ ok: false, error: "Missing env: NEXT_PUBLIC_API_BASE (or CORE_API_BASE)" },{ status: 500 });}const url = `${base.replace(/\/$/, "")}/health`;const t = withTimeout(8000);try {const res = await fetch(url, {cache: "no-store",signal: t.controller.signal,headers: { Accept: "application/json" },});const text = await res.text();let data: any = text;try {data = JSON.parse(text);} catch {}return NextResponse.json({ ok: res.ok, status: res.status, url, data },{ status: res.ok ? 200 : 502 });} catch (e: any) {// fetch failed gibi saçma mesajları kırıp içini gösteriyoruzreturn NextResponse.json({ok: false,url,error: String(e?.message || e),name: e?.name,cause: e?.cause ? String(e.cause) : null,},{ status: 502 });} finally {t.clear();}}1234567891011121314151617181920212223242526272829303132import { NextResponse } from "next/server";export async function GET() {const base = process.env.NEXT_PUBLIC_API_BASE; // HF Space variableif (!base) {return NextResponse.json({ ok: false, error: "NEXT_PUBLIC_API_BASE missing" },{ status: 500 });}try {const res = await fetch(`${base.replace(/\/$/, "")}/health`, {cache: "no-store",});const text = await res.text(); // json parse fail olursa görmek içinlet data: any;try {data = JSON.parse(text);} catch {data = { raw: text };}return NextResponse.json({ ok: res.ok, status: res.status, data });} catch (e: any) {return NextResponse.json({ ok: false, error: e?.message ?? "proxy fetch failed" },{ status: 500 });}}
app/api/core-health/route.ts+11-40