Files
NianAIGC/app/api/ready/route.ts

32 lines
993 B
TypeScript

import { NextResponse } from "next/server";
import { checkDatabaseReadiness, getDatabaseStatus } from "@/lib/server/database";
export const runtime = "nodejs";
const READINESS_TIMEOUT_MS = 3_000;
export async function GET() {
let database: { backend: "local" | "postgres" | "invalid"; configured: boolean };
try {
database = getDatabaseStatus();
} catch {
database = { backend: "invalid", configured: false };
return NextResponse.json({ ok: false, database }, { status: 503 });
}
let timeout: ReturnType<typeof setTimeout> | undefined;
try {
await Promise.race([
checkDatabaseReadiness(),
new Promise<never>((_, reject) => {
timeout = setTimeout(() => reject(new Error("Database readiness timed out")), READINESS_TIMEOUT_MS);
})
]);
return NextResponse.json({ ok: true, database });
} catch {
return NextResponse.json({ ok: false, database }, { status: 503 });
} finally {
if (timeout) clearTimeout(timeout);
}
}