diff --git a/go.mod b/go.mod index d99af4c9..4b013c09 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/TwiN/gocache v1.2.4 - github.com/TwiN/health v1.1.0 + github.com/TwiN/health v1.3.0 github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-ping/ping v0.0.0-20210911151512-381826476871 diff --git a/go.sum b/go.sum index c47ce0e3..36e86ed5 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/TwiN/gocache v1.2.4 h1:AfJ1YRcxtQ/zZEN61URDwk/dwFG7LSRenU5qIm9dQzo= github.com/TwiN/gocache v1.2.4/go.mod h1:BjabsQQy6z5uHDorHa4LJVPEzFeitLIDbCtdv3gc1gA= -github.com/TwiN/health v1.1.0 h1:IbXV4b5VPxzfIqOPiP/19JdBNFYM0oEDReLbUazhb2k= -github.com/TwiN/health v1.1.0/go.mod h1:Bt+lEvSi6C/9NWb7OoGmUmgtS4dfPeMM9EINnURv5dE= +github.com/TwiN/health v1.3.0 h1:xw90rZqg0NH5MRkVHzlgtDdP+EQd43v3yMqQVtYlGHg= +github.com/TwiN/health v1.3.0/go.mod h1:Bt+lEvSi6C/9NWb7OoGmUmgtS4dfPeMM9EINnURv5dE= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/vendor/github.com/TwiN/health/README.md b/vendor/github.com/TwiN/health/README.md index 9cd6f659..a22a0274 100644 --- a/vendor/github.com/TwiN/health/README.md +++ b/vendor/github.com/TwiN/health/README.md @@ -33,16 +33,24 @@ If you prefer using JSON, however, you may initialize the health handler like so ```go router.Handle("/health", health.Handler().WithJSON(true)) ``` -The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively. +The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively, +unless there is a reason, in which case a reason set to `because` would return `{"status":"UP", "reason":"because"}` +and `{"status":"DOWN", "reason":"because"}` respectively. To change the health of the application, you can use `health.SetStatus()` where `` is one `health.Up` or `health.Down`: - ```go health.SetStatus(health.Up) health.SetStatus(health.Down) ``` +As for the reason: +```go +health.SetReason("database is unreachable") +``` + +Generally speaking, you'd only want to include a reason if the status is `Down`, but you can do as you desire. + ### Complete example ```go diff --git a/vendor/github.com/TwiN/health/health.go b/vendor/github.com/TwiN/health/health.go index 1840edca..bb33379e 100644 --- a/vendor/github.com/TwiN/health/health.go +++ b/vendor/github.com/TwiN/health/health.go @@ -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() } diff --git a/vendor/modules.txt b/vendor/modules.txt index aac43121..bce5410a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # github.com/TwiN/gocache v1.2.4 ## explicit; go 1.16 github.com/TwiN/gocache -# github.com/TwiN/health v1.1.0 +# github.com/TwiN/health v1.3.0 ## explicit; go 1.17 github.com/TwiN/health # github.com/beorn7/perks v1.0.1