File size: 1,861 Bytes
a5b7ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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"));