40 lines
777 B
JavaScript
40 lines
777 B
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const http = require("node:http");
|
|
|
|
const port = Number.parseInt(process.env.PORT || "3000", 10);
|
|
const path = process.env.HEALTHCHECK_PATH || "/api/health";
|
|
const timeoutMs = Number.parseInt(process.env.HEALTHCHECK_TIMEOUT_MS || "3000", 10);
|
|
|
|
const req = http.request(
|
|
{
|
|
host: "127.0.0.1",
|
|
port,
|
|
path,
|
|
method: "GET",
|
|
timeout: timeoutMs
|
|
},
|
|
(res) => {
|
|
// Drain the response so the socket can close cleanly.
|
|
res.resume();
|
|
|
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 400) {
|
|
process.exit(0);
|
|
return;
|
|
}
|
|
|
|
process.exit(1);
|
|
}
|
|
);
|
|
|
|
req.on("timeout", () => {
|
|
req.destroy(new Error("healthcheck timeout"));
|
|
});
|
|
|
|
req.on("error", () => {
|
|
process.exit(1);
|
|
});
|
|
|
|
req.end();
|