Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/web/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NextResponse } from 'next/server';
import { getDb } from '@/lib/db';

// Never cache: a cached 200 would keep reporting health after the app goes away.
export const dynamic = 'force-dynamic';

const NO_STORE = { 'Cache-Control': 'no-store' };

/**
* Liveness for status.profullstack.com: the app answers and the database
* answers `SELECT 1` within 3s. Unlike /api/health/db it never says why it
* failed.
*/
export async function GET() {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
await Promise.race([
getDb().sql`SELECT 1`,
new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error('timeout')), 3000);
}),
]);
return NextResponse.json({ status: 'ok', db: 'ok' }, { headers: NO_STORE });
} catch {
return NextResponse.json({ status: 'error', db: 'down' }, { status: 503, headers: NO_STORE });
} finally {
clearTimeout(timer);
}
}
Loading