From 1d5c1897a372b973235a18459ae3b1f9a5144989 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Tue, 3 Mar 2020 13:34:26 +0300 Subject: [PATCH] reusable health checkers endpoints --- health.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 health.go diff --git a/health.go b/health.go new file mode 100644 index 0000000..708bbb7 --- /dev/null +++ b/health.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/fasthttp/router" + "github.com/valyala/fasthttp" + "go.uber.org/atomic" +) + +const ( + healthyState = "NeoFS HTTP Gateway is " + defaultContentType = "text/plain; charset=utf-8" +) + +func attachHealthy(r *router.Router, e *atomic.Error) { + r.GET("/-/ready", func(ctx *fasthttp.RequestCtx) { + ctx.SetStatusCode(fasthttp.StatusOK) + ctx.SetBodyString(healthyState + "ready") + }) + + r.GET("/-/healthy", func(c *fasthttp.RequestCtx) { + code := fasthttp.StatusOK + msg := "healthy" + + if err := e.Load(); err != nil { + msg = "unhealthy: " + err.Error() + code = fasthttp.StatusBadRequest + } + + c.Response.Reset() + c.SetStatusCode(code) + c.SetContentType(defaultContentType) + c.SetBodyString(healthyState + msg) + }) +}