Files
bux-lang/examples/http_health.bux
T
dimgigov a785747c37
ci / build (ubuntu) (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
feat: Linux/cloud platform stack (TLS, registry, static/cross, selfhost PM)
Ship the QUALITY_PLAN platform focus: thin/minimal runtime, --static/--target,
Nexus HTTPS/mTLS with graceful stop, lock checksums + install --locked,
selfhost registry (search/add/HTTP), containers, and CI smokes for cloud path.
2026-07-23 23:00:55 +03:00

79 lines
2.2 KiB
Plaintext

// Session 78 — tiny health HTTP server for containers / ops probes.
// Needs full POSIX runtime (sockets). Not TLS — use Nexus for HTTPS.
import Std::Io::{Print, PrintLine, PrintInt};
import Std::Net::{Net_Create, Net_SetReuse, Net_Bind, Net_Listen, Net_Accept, Net_Send, Net_Recv, Net_Close};
import Std::String::{String_Len, String_Contains, String_FromInt, String_Concat, String_ToInt};
import Std::Os::{Os_GetEnv, Os_InstallStopHandlers, Os_ShouldStop, Os_SetStopListenFd};
func HealthBody() -> String {
return "{\"status\":\"ok\",\"server\":\"bux-health\",\"version\":\"0.1.0\"}\n";
}
func BuildHttp(body: String) -> String {
let n: int = String_Len(body) as int;
return String_Concat(
String_Concat(
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ",
String_FromInt(n as int64)
),
String_Concat("\r\nConnection: close\r\n\r\n", body)
);
}
func Main() -> int {
var port: int = 8080;
let pe: String = Os_GetEnv("HEALTH_PORT");
if String_Len(pe) > 0 {
let p: int64 = String_ToInt(pe);
if p > 0 && p < 65536 {
port = p as int;
}
}
var bind: String = "0.0.0.0";
let bindEnv: String = Os_GetEnv("HEALTH_BIND");
if String_Len(bindEnv) > 0 {
bind = bindEnv;
}
Os_InstallStopHandlers();
let fd: int = Net_Create();
if fd < 0 {
PrintLine("socket failed");
return 1;
}
discard Net_SetReuse(fd);
if !Net_Bind(fd, bind, port) {
PrintLine("bind failed");
return 1;
}
if !Net_Listen(fd, 64) {
PrintLine("listen failed");
return 1;
}
Os_SetStopListenFd(fd);
Print("health listening on http://");
Print(bind);
Print(":");
PrintInt(port);
PrintLine("");
let body: String = HealthBody();
let resp: String = BuildHttp(body);
while !Os_ShouldStop() {
let c: int = Net_Accept(fd);
if c < 0 {
if Os_ShouldStop() { break; }
continue;
}
let raw: String = Net_Recv(c, 4096);
if String_Contains(raw, "GET ") {
discard Net_Send(c, resp);
}
Net_Close(c);
}
Net_Close(fd);
PrintLine("health: stopped");
return 0;
}