| 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}`); |
| }); |
|
|