File size: 1,026 Bytes
dde0e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}`);
});