// The capsule's main internal port: GET /health is 200 while ttyd (the app, port 7681) // accepts connections, 503 otherwise. A TCP connect, so ttyd logs nothing for it. // Nothing else lives here; the terminal is the app port. const http = require("http"); const net = require("net"); function ttydUp() { return new Promise((resolve) => { const sock = net.connect({ host: "127.0.0.1", port: 7681, timeout: 2000 }); sock.on("connect", () => { sock.destroy(); resolve(true); }); sock.on("error", () => resolve(false)); sock.on("timeout", () => { sock.destroy(); resolve(false); }); }); } http.createServer(async (req, res) => { if (req.method === "GET" && req.url === "/health") { const ok = await ttydUp(); res.writeHead(ok ? 200 : 503, { "Content-Type": "application/json" }); res.end(JSON.stringify({ status: ok ? "ok" : "starting" })); return; } res.writeHead(404, { "Content-Type": "application/json" }); res.end('{"error":"not found"}'); }).listen(Number(process.env.M64_PORT || 9500), "0.0.0.0");