04965eb89d
Remove startsWith('/auth') guard that caused non-auth paths to hang with
no response. Better-Auth already handles /health and /auth/health are
explicitly short-circuited before the handler. Add test asserting unknown
paths receive a terminal response within 1s.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { createServer } from "node:http";
|
|
import { toNodeHandler } from "better-auth/node";
|
|
import { auth, pool } from "./auth.js";
|
|
|
|
const port = parseInt(process.env.PORT ?? "3001", 10);
|
|
|
|
const handler = toNodeHandler(auth);
|
|
|
|
const server = createServer(async (req, res) => {
|
|
// Health check
|
|
if ((req.url === "/health" || req.url === "/auth/health") && req.method === "GET") {
|
|
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: "reachable" }));
|
|
} catch {
|
|
res.writeHead(503, { "Content-Type": "application/json" });
|
|
res.end(JSON.stringify({ status: "error", db: "unreachable" }));
|
|
}
|
|
return;
|
|
}
|
|
|
|
// All other routes handled by Better-Auth (returns 404 for unknown paths)
|
|
await handler(req, res);
|
|
});
|
|
|
|
server.listen(port, "0.0.0.0", () => {
|
|
console.log(`CartSnitch auth service listening on port ${port}`);
|
|
});
|