frostfs-http-gw/health.go

36 lines
748 B
Go
Raw Normal View History

2020-03-03 10:34:26 +00:00
package main
import (
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
2020-11-09 13:43:23 +00:00
type stater func() error
2020-03-03 10:34:26 +00:00
const (
healthyState = "NeoFS HTTP Gateway is "
defaultContentType = "text/plain; charset=utf-8"
)
2020-11-09 13:43:23 +00:00
func attachHealthy(r *router.Router, e stater) {
2020-03-31 08:37:10 +00:00
r.GET("/-/ready/", func(ctx *fasthttp.RequestCtx) {
2020-03-03 10:34:26 +00:00
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBodyString(healthyState + "ready")
})
2020-03-31 08:37:10 +00:00
r.GET("/-/healthy/", func(c *fasthttp.RequestCtx) {
2020-03-03 10:34:26 +00:00
code := fasthttp.StatusOK
msg := "healthy"
2020-11-09 13:43:23 +00:00
if err := e(); err != nil {
2020-03-03 10:34:26 +00:00
msg = "unhealthy: " + err.Error()
code = fasthttp.StatusBadRequest
}
c.Response.Reset()
c.SetStatusCode(code)
c.SetContentType(defaultContentType)
c.SetBodyString(healthyState + msg)
})
}