2021-02-13 05:29:21 +01:00
|
|
|
package health
|
|
|
|
|
|
|
|
import "net/http"
|
|
|
|
|
|
|
|
var (
|
|
|
|
handler = &healthHandler{
|
|
|
|
useJSON: false,
|
|
|
|
status: Up,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
// healthHandler is the HTTP handler for serving the health endpoint
|
2021-02-13 05:29:21 +01:00
|
|
|
type healthHandler struct {
|
|
|
|
useJSON bool
|
|
|
|
status Status
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithJSON configures whether the handler should output a response in JSON or in raw text
|
|
|
|
//
|
|
|
|
// Defaults to false
|
|
|
|
func (h *healthHandler) WithJSON(v bool) *healthHandler {
|
|
|
|
h.useJSON = v
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
// ServeHTTP serves the HTTP request for the health handler
|
|
|
|
func (h healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) {
|
2021-02-13 05:29:21 +01:00
|
|
|
var status int
|
|
|
|
var body []byte
|
|
|
|
if h.status == Up {
|
|
|
|
status = http.StatusOK
|
|
|
|
} else {
|
|
|
|
status = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
if h.useJSON {
|
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
body = []byte(`{"status":"` + h.status + `"}`)
|
|
|
|
} else {
|
|
|
|
body = []byte(h.status)
|
|
|
|
}
|
|
|
|
writer.WriteHeader(status)
|
|
|
|
_, _ = writer.Write(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler retrieves the health handler
|
|
|
|
func Handler() *healthHandler {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStatus sets the status to be reflected by the health handler
|
|
|
|
func SetStatus(status Status) {
|
|
|
|
handler.status = status
|
|
|
|
}
|