frostfs-s3-gw/cmd/s3-gw/app_healthy.go

47 lines
1 KiB
Go
Raw Normal View History

2020-07-06 09:18:16 +00:00
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
// Healthy is a health check interface.
2020-07-12 23:00:47 +00:00
type Healthy interface {
Status() error
}
2020-07-06 09:18:16 +00:00
const (
healthyState = "NeoFS S3 Gateway is "
hdrContentType = "Content-Type"
defaultContentType = "text/plain; charset=utf-8"
2020-07-06 09:18:16 +00:00
)
//nolint:deadcode,unused // TODO
2020-07-12 23:00:47 +00:00
func attachHealthy(r *mux.Router, h Healthy) {
healthy := r.PathPrefix(systemPath + "/-").
Subrouter().
StrictSlash(true)
healthy.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(hdrContentType, defaultContentType)
2020-07-06 09:18:16 +00:00
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, healthyState+"ready")
})
healthy.HandleFunc("/healthy", func(w http.ResponseWriter, r *http.Request) {
2020-07-06 09:18:16 +00:00
code := http.StatusOK
msg := "healthy"
2020-07-12 23:00:47 +00:00
if err := h.Status(); err != nil {
2020-07-06 09:18:16 +00:00
msg = "unhealthy: " + err.Error()
code = http.StatusBadRequest
}
w.Header().Set(hdrContentType, defaultContentType)
2020-07-06 09:18:16 +00:00
w.WriteHeader(code)
_, _ = fmt.Fprintln(w, healthyState+msg)
})
}