test2 / server.mjs
rtrm's picture
rtrm HF Staff
feat: deploy sns poc helper
dde0e3e unverified
Raw
History Blame Contribute Delete
1.03 kB
import fs from "node:fs";
import http from "node:http";
import { fileURLToPath } from "node:url";
const port = Number(process.env.PORT ?? 7860);
const publicPem = fs.readFileSync(fileURLToPath(new URL("./cert.pem", import.meta.url)), "utf8");
function json(res, statusCode, payload) {
res.writeHead(statusCode, { "content-type": "application/json" });
res.end(JSON.stringify(payload, null, 2));
}
function text(res, statusCode, payload, contentType = "text/plain") {
res.writeHead(statusCode, { "content-type": contentType });
res.end(payload);
}
const server = http.createServer((req, res) => {
const url = new URL(req.url ?? "/", "http://localhost");
if (req.method === "GET" && url.pathname === "/healthz") {
json(res, 200, { ok: true });
return;
}
if (req.method === "GET" && url.pathname === "/cert.pem") {
text(res, 200, publicPem, "application/x-pem-file");
return;
}
json(res, 404, { error: "not found" });
});
server.listen(port, "0.0.0.0", () => {
console.log(`Listening on ${port}`);
});