Files
pikasTech-HWLAB/cmd/hwlab-edge-proxy/main.mjs
T
2026-05-21 18:14:58 +00:00

68 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
import {
healthPayload,
listen,
proxyHttpRequest,
resolveHostPort,
sendJson
} from "../../internal/dev-entrypoint/http.mjs";
import { createServer } from "node:http";
const serviceId = "hwlab-edge-proxy";
const upstream = process.env.HWLAB_EDGE_UPSTREAM || "http://hwlab-cloud-api.hwlab-dev.svc.cluster.local:6667";
const { host, port } = resolveHostPort({
listenEnv: "HWLAB_EDGE_LISTEN",
hostEnv: "HWLAB_EDGE_HOST",
portEnv: "HWLAB_EDGE_PORT",
fallbackPort: 6667
});
function proxyDetails() {
return {
upstream,
mode: "dev-edge-proxy"
};
}
const server = createServer((request, response) => {
const url = new URL(request.url || "/", "http://hwlab-edge-proxy.local");
if (request.method === "GET" && (url.pathname === "/health" || url.pathname === "/health/live")) {
sendJson(response, 200, healthPayload({
serviceId,
role: "public-dev-ingress",
details: proxyDetails()
}));
return;
}
if (request.method === "GET" && (url.pathname === "/status" || url.pathname === "/routes")) {
sendJson(response, 200, {
serviceId,
environment: "dev",
status: "ok",
upstream,
routes: [
{
pathPrefix: "/",
upstream,
mode: "http-proxy"
}
]
});
return;
}
if (request.method === "GET" && url.pathname === "/help") {
sendJson(response, 200, {
serviceId,
commands: ["GET /health", "GET /health/live", "GET /status", "GET /routes"]
});
return;
}
proxyHttpRequest({ request, response, upstream });
});
listen(server, { serviceId, host, port });