README
¶
handlers
The health
library comes bundled with some no-thrills HTTP handlers that should
fit most use cases.
Usage
After setting up an instance of health
, register a healthcheck endpoint and point
it at a handler func.
import (
"github.com/InVisionApp/go-health"
"github.com/InVisionApp/go-health/checkers"
"github.com/InVisionApp/go-health/handlers"
)
// create and configure a new health instance
h := health.New()
h.AddChecks(...)
// Register a new endpoint and have it use a pre-built handler
http.HandleFunc("/healthcheck", handlers.NewJSONHandlerFunc(h, nil))
http.ListenAndServe(":8080", nil)
Behavior
If any check fails that is configured as fatal
- the handler will return a
http.StatusInternalServerError
; otherwise, it will return a http.StatusOK
.
handlers.NewJSONHandlerFunc
output example
{
"details": {
"bad-check": {
"name": "bad-check",
"status": "failed",
"error": "Ran into error while performing 'GET' request: Get google.com: unsupported protocol scheme \"\"",
"check_time": "2017-12-05T19:17:23.691637151-08:00"
},
"good-check": {
"name": "good-check",
"status": "ok",
"check_time": "2017-12-05T19:17:23.857481271-08:00"
}
},
"status": "ok"
}
handlers.NewBasicHandlerFunc
example output
ok || failed
Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewBasicHandlerFunc ¶
func NewBasicHandlerFunc(h health.IHealth) http.HandlerFunc
NewBasicHandlerFunc will return an `http.HandlerFunc` that will write `ok` string + `http.StatusOK` to `rw“ if `h.Failed()` returns `false`; returns `error` + `http.StatusInternalServerError` if `h.Failed()` returns `true`.
func NewJSONHandlerFunc ¶
func NewJSONHandlerFunc(h health.IHealth, custom map[string]interface{}) http.HandlerFunc
NewJSONHandlerFunc will return an `http.HandlerFunc` that will marshal and write the contents of `h.StateMapInterface()` to `rw` and set status code to
`http.StatusOK` if `h.Failed()` is `false` OR set status code to
`http.StatusInternalServerError` if `h.Failed` is `true`. It also accepts a set of optional custom fields to be added to the final JSON body
Types ¶
This section is empty.