From c7366f625483c6dc06e787d3c8b240d3f6ab364c Mon Sep 17 00:00:00 2001 From: Paperclip Date: Tue, 14 Apr 2026 16:35:24 +0000 Subject: [PATCH] fix: restore DB connectivity check to auth health endpoint Co-Authored-By: Paperclip --- src/auth.ts | 2 +- src/index.ts | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 202802c..c882aac 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -17,7 +17,7 @@ if (!databaseUrl) { ); } -const pool = new Pool({ +export const pool = new Pool({ connectionString: databaseUrl ?? "postgresql://cartsnitch:cartsnitch@localhost:5432/cartsnitch", }); diff --git a/src/index.ts b/src/index.ts index 843a97c..708d91d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { createServer } from "node:http"; import { toNodeHandler } from "better-auth/node"; -import { auth } from "./auth.js"; +import { auth, pool } from "./auth.js"; const port = parseInt(process.env.PORT ?? "3001", 10); @@ -9,8 +9,22 @@ const handler = toNodeHandler(auth); const server = createServer(async (req, res) => { // Health check if (req.url === "/health" && req.method === "GET") { - res.writeHead(200, { "Content-Type": "application/json" }); - res.end(JSON.stringify({ status: "ok" })); + try { + const client = await pool.connect(); + try { + await Promise.race([ + client.query("SELECT 1"), + new Promise((_, reject) => setTimeout(() => reject(new Error("DB timeout")), 2000)), + ]); + } finally { + client.release(); + } + res.writeHead(200, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ status: "ok", db: "connected" })); + } catch { + res.writeHead(503, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ status: "error", db: "unreachable" })); + } return; }