import { NextResponse } from "next/server" import { uptimeSeconds, TOWER_VERSION } from "@/app/lib/fleet/health" import { FLEET_SPOOL_DIR, FLEET_ORG_PATH, loadOrg } from "@/app/lib/fleet/fleetSource" import { existsSync } from "node:fs" export const dynamic = "force-dynamic" /** * GET /healthz — Readiness/Liveness des Tower-Service (Karte it1-tower-03). * 200 = Kernquellen (Spool-PVC, org.yaml) verfuegbar; 503 = eine Kernquelle tot. * JSON statt HTML, damit Uptime-Monitoring den Grund maschinell lesen kann. */ export async function GET() { const problems: string[] = [] const spoolOk = existsSync(FLEET_SPOOL_DIR) if (!spoolOk) problems.push(`spool fehlt: ${FLEET_SPOOL_DIR}`) if (!existsSync(FLEET_ORG_PATH)) { problems.push(`org.yaml fehlt: ${FLEET_ORG_PATH}`) } else { try { if (loadOrg().members.length === 0) problems.push("org.yaml ohne members") } catch (e) { problems.push(`org.yaml unlesbar: ${e instanceof Error ? e.message : String(e)}`) } } const ok = problems.length === 0 return NextResponse.json( { status: ok ? "ok" : "unhealthy", version: TOWER_VERSION, uptime_seconds: uptimeSeconds(), generated_at: new Date().toISOString(), problems, }, { status: ok ? 200 : 503, headers: { "Cache-Control": "no-store" } } ) }