Update TwiN/health to v1.3.0
This commit is contained in:
74
vendor/github.com/TwiN/health/health.go
generated
vendored
74
vendor/github.com/TwiN/health/health.go
generated
vendored
@ -1,6 +1,7 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
@ -12,12 +13,20 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
type responseBody struct {
|
||||
Status string `json:"status"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// healthHandler is the HTTP handler for serving the health endpoint
|
||||
type healthHandler struct {
|
||||
useJSON bool
|
||||
status Status
|
||||
useJSON bool
|
||||
resetReasonOnUp bool
|
||||
|
||||
sync.RWMutex
|
||||
status Status
|
||||
reason string
|
||||
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
// WithJSON configures whether the handler should output a response in JSON or in raw text
|
||||
@ -32,34 +41,29 @@ func (h *healthHandler) WithJSON(v bool) *healthHandler {
|
||||
func (h *healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) {
|
||||
var statusCode int
|
||||
var body []byte
|
||||
handlerStatus := h.getStatus()
|
||||
if handlerStatus == Up {
|
||||
h.mutex.RLock()
|
||||
status, reason, useJSON := h.status, h.reason, h.useJSON
|
||||
h.mutex.RUnlock()
|
||||
if status == Up {
|
||||
statusCode = http.StatusOK
|
||||
} else {
|
||||
statusCode = http.StatusInternalServerError
|
||||
}
|
||||
if h.useJSON {
|
||||
if useJSON {
|
||||
// We can safely ignore the error here because we know that both values are strings, therefore are supported encoders.
|
||||
body, _ = json.Marshal(responseBody{Status: string(status), Reason: reason})
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
body = []byte(`{"status":"` + handlerStatus + `"}`)
|
||||
} else {
|
||||
body = []byte(handlerStatus)
|
||||
if len(reason) == 0 {
|
||||
body = []byte(status)
|
||||
} else {
|
||||
body = []byte(string(status) + ": " + reason)
|
||||
}
|
||||
}
|
||||
writer.WriteHeader(statusCode)
|
||||
_, _ = writer.Write(body)
|
||||
}
|
||||
|
||||
func (h *healthHandler) getStatus() Status {
|
||||
h.Lock()
|
||||
defer h.Unlock()
|
||||
return h.status
|
||||
}
|
||||
|
||||
func (h *healthHandler) setStatus(status Status) {
|
||||
h.Lock()
|
||||
h.status = status
|
||||
h.Unlock()
|
||||
}
|
||||
|
||||
// Handler retrieves the health handler
|
||||
func Handler() *healthHandler {
|
||||
return handler
|
||||
@ -67,10 +71,36 @@ func Handler() *healthHandler {
|
||||
|
||||
// GetStatus retrieves the current status returned by the health handler
|
||||
func GetStatus() Status {
|
||||
return handler.getStatus()
|
||||
handler.mutex.RLock()
|
||||
defer handler.mutex.RUnlock()
|
||||
return handler.status
|
||||
}
|
||||
|
||||
// SetStatus sets the status to be returned by the health handler
|
||||
func SetStatus(status Status) {
|
||||
handler.setStatus(status)
|
||||
handler.mutex.Lock()
|
||||
handler.status = status
|
||||
handler.mutex.Unlock()
|
||||
}
|
||||
|
||||
// GetReason retrieves the current status returned by the health handler
|
||||
func GetReason() string {
|
||||
handler.mutex.RLock()
|
||||
defer handler.mutex.RUnlock()
|
||||
return handler.reason
|
||||
}
|
||||
|
||||
// SetReason sets a reason for the current status to be returned by the health handler
|
||||
func SetReason(reason string) {
|
||||
handler.mutex.Lock()
|
||||
handler.reason = reason
|
||||
handler.mutex.Unlock()
|
||||
}
|
||||
|
||||
// SetStatusAndReason sets the status and reason to be returned by the health handler
|
||||
func SetStatusAndReason(status Status, reason string) {
|
||||
handler.mutex.Lock()
|
||||
handler.status = status
|
||||
handler.reason = reason
|
||||
handler.mutex.Unlock()
|
||||
}
|
||||
|
Reference in New Issue
Block a user