m64_7796c3cb05e5 / health.js
25 lines, 1.0 KBraw
1// The capsule's main internal port: GET /health is 200 while ttyd (the app, port 7681)2// accepts connections, 503 otherwise. A TCP connect, so ttyd logs nothing for it.3// Nothing else lives here; the terminal is the app port.4const http = require("http");5const net = require("net");67function ttydUp() {8 return new Promise((resolve) => {9 const sock = net.connect({ host: "127.0.0.1", port: 7681, timeout: 2000 });10 sock.on("connect", () => { sock.destroy(); resolve(true); });11 sock.on("error", () => resolve(false));12 sock.on("timeout", () => { sock.destroy(); resolve(false); });13 });14}1516http.createServer(async (req, res) => {17 if (req.method === "GET" && req.url === "/health") {18 const ok = await ttydUp();19 res.writeHead(ok ? 200 : 503, { "Content-Type": "application/json" });20 res.end(JSON.stringify({ status: ok ? "ok" : "starting" }));21 return;22 }23 res.writeHead(404, { "Content-Type": "application/json" });24 res.end('{"error":"not found"}');25}).listen(Number(process.env.M64_PORT || 9500), "0.0.0.0");