Skip to content

Commit f74ce4f

Browse files
authored
Merge pull request #2 from RMCampos/copilot/create-health-check-api
Add /api/health endpoint for service health monitoring
2 parents 03a3f58 + e4af45b commit f74ce4f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

app/api/health/route.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NextResponse } from "next/server"
2+
import { sql } from "@/lib/db"
3+
4+
export async function GET() {
5+
const appVersion = process.env.NEXT_PUBLIC_APP_VERSION || "unknown"
6+
7+
// Check database connection
8+
let databaseStatus = "DOWN"
9+
let databaseError: string | null = null
10+
11+
try {
12+
// Simple query to test the database connection
13+
await sql`SELECT 1 as test`
14+
databaseStatus = "UP"
15+
} catch (error) {
16+
console.error("Health check - Database connection failed:", error)
17+
databaseError = error instanceof Error ? error.message : String(error)
18+
}
19+
20+
const response = {
21+
status: "UP",
22+
database: databaseStatus,
23+
version: appVersion,
24+
}
25+
26+
// If database is down, include error details and return 503
27+
if (databaseStatus === "DOWN") {
28+
return NextResponse.json(
29+
{
30+
...response,
31+
databaseError,
32+
},
33+
{ status: 503 }
34+
)
35+
}
36+
37+
return NextResponse.json(response)
38+
}

0 commit comments

Comments
 (0)