-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
42 lines (39 loc) · 1.11 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package healthcheck
import (
"context"
"encoding/json"
"net/http"
)
// handler will handle health check requests.
// Return 200 if all checkers pass, otherwise 503.
// If no parameter set, handler will only return the status code and no body.
// If detail query parameter set, it will show the detail of each checker and
// their errors, or OK status. The body is in JSON format.
func (h *HealthCheck) handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
errs := h.check(ctx)
if len(errs) == 0 {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
_, ok := r.URL.Query()["detail"]
if ok {
h.handlerDetail(ctx, w, errs)
}
}
// handlerDetail writes json version of details of checkers to the response.
func (h *HealthCheck) handlerDetail(_ context.Context, w http.ResponseWriter, errs map[string]error) {
result := make(map[string]string)
for name := range h.checkers {
err, ok := errs[name]
if ok {
result[name] = err.Error()
} else {
result[name] = "OK"
}
}
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
_ = encoder.Encode(result)
}