From 19f0d3d0c1b171ebc25176f777d034ffaa98a4be Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Mon, 3 Mar 2025 07:17:47 -0500 Subject: cmd/anubis: implement health check (#685) Closes #681 Signed-off-by: Xe Iaso --- Earthfile | 4 ++++ cmd/anubis/main.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Earthfile b/Earthfile index 740d0ec..65529b4 100644 --- a/Earthfile +++ b/Earthfile @@ -72,6 +72,8 @@ anubis-amd64: CMD ["/app/bin/anubis"] USER 1000:1000 + HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 CMD ["/app/bin/anubis", "--healthcheck"] + SAVE IMAGE --push ghcr.io/xe/x/anubis:latest anubis-arm64: @@ -79,6 +81,8 @@ anubis-arm64: CMD ["/app/bin/anubis"] USER 1000:1000 + HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 CMD ["/app/bin/anubis", "--healthcheck"] + SAVE IMAGE --push ghcr.io/xe/x/anubis:latest anubis: diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go index 9b39aab..69be826 100644 --- a/cmd/anubis/main.go +++ b/cmd/anubis/main.go @@ -42,6 +42,7 @@ var ( robotsTxt = flag.Bool("serve-robots-txt", false, "serve a robots.txt file that disallows all robots") policyFname = flag.String("policy-fname", "", "full path to anubis policy document (defaults to a sensible built-in policy)") target = flag.String("target", "http://localhost:3923", "target to reverse proxy to") + healthcheck = flag.Bool("healthcheck", false, "run a health check against Anubis") //go:embed static botPolicies.json static embed.FS @@ -84,9 +85,30 @@ const ( //go:generate zstd -f -k --ultra -22 static/js/main.mjs //go:generate brotli -fZk static/js/main.mjs +func doHealthCheck() error { + resp, err := http.Get("http://localhost" + *metricsBind + "/metrics") + if err != nil { + return fmt.Errorf("failed to fetch metrics: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + return nil +} + func main() { internal.HandleStartup() + if *healthcheck { + if err := doHealthCheck(); err != nil { + log.Fatal(err) + } + return + } + s, err := New(*target, *policyFname) if err != nil { log.Fatal(err) -- cgit v1.2.3