diff options
| author | Xe Iaso <me@xeiaso.net> | 2025-03-03 07:17:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-03 12:17:47 +0000 |
| commit | 19f0d3d0c1b171ebc25176f777d034ffaa98a4be (patch) | |
| tree | dfb2f65eb35a232449796934e170cb1bafe15668 | |
| parent | 6ccc79e1abb29e91e654ba0cc55858b6ffa76d14 (diff) | |
| download | x-19f0d3d0c1b171ebc25176f777d034ffaa98a4be.tar.xz x-19f0d3d0c1b171ebc25176f777d034ffaa98a4be.zip | |
cmd/anubis: implement health check (#685)
Closes #681
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | Earthfile | 4 | ||||
| -rw-r--r-- | cmd/anubis/main.go | 22 |
2 files changed, 26 insertions, 0 deletions
@@ -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) |
