const fs = require("fs"); const http = require("http"); const host = "0.0.0.0"; const port = Number(process.env.PORT || 7860); const startedAt = new Date(); function safeRead(filePath) { try { return fs.readFileSync(filePath, "utf8").trim(); } catch { return "unknown"; } } const sourceCommit = safeRead("/space/bootstrap/SOURCE_COMMIT_SHA.txt"); const sourceCommitDate = safeRead("/space/bootstrap/LAST_COMMIT_DATE.txt"); function sendJson(res, statusCode, payload) { const body = JSON.stringify(payload, null, 2); res.writeHead(statusCode, { "Content-Type": "application/json; charset=utf-8", "Content-Length": Buffer.byteLength(body), }); res.end(body); } const server = http.createServer((req, res) => { const now = new Date(); const uptimeSec = Math.floor((now.getTime() - startedAt.getTime()) / 1000); if (req.url === "/healthz" || req.url === "/readyz") { return sendJson(res, 200, { ok: true, status: "healthy", uptimeSec, now: now.toISOString(), }); } if (req.url === "/meta") { return sendJson(res, 200, { sourceCommit, sourceCommitDate, repoSlug: process.env.GH_REPO_SLUG || "unset", repoRef: process.env.GH_REPO_REF || "main", appSubdir: process.env.GH_APP_SUBDIR || ".", }); } return sendJson(res, 200, { message: "HF bootstrap server is running", endpoints: ["/", "/healthz", "/readyz", "/meta"], sourceCommit, sourceCommitDate, uptimeSec, }); }); server.listen(port, host, () => { process.stdout.write(`[hf-http] listening on http://${host}:${port}\n`); }); function shutdown(signal) { process.stdout.write(`[hf-http] ${signal} received, shutting down\n`); server.close(() => process.exit(0)); } process.on("SIGTERM", () => shutdown("SIGTERM")); process.on("SIGINT", () => shutdown("SIGINT"));