-
Notifications
You must be signed in to change notification settings - Fork 58
/
handlers.go
96 lines (79 loc) · 2.58 KB
/
handlers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package handlers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/InVisionApp/go-health"
)
type jsonStatus struct {
Message string `json:"message"`
Status string `json:"status"`
}
// 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 NewBasicHandlerFunc(h health.IHealth) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
status := http.StatusOK
body := "ok"
if h.Failed() {
status = http.StatusInternalServerError
body = "failed"
}
rw.WriteHeader(status)
rw.Write([]byte(body))
})
}
// 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
func NewJSONHandlerFunc(h health.IHealth, custom map[string]interface{}) http.HandlerFunc {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
states, failed, err := h.State()
if err != nil {
writeJSONStatus(rw, "error", fmt.Sprintf("Unable to fetch states: %v", err), http.StatusOK)
return
}
msg := "ok"
statusCode := http.StatusOK
// There may be an _initial_ delay in display healthcheck data as the
// healthchecks will only begin firing at "initialTime + checkIntervalTime"
if len(states) == 0 {
writeJSONStatus(rw, msg, "Healthcheck spinning up", statusCode)
return
}
if failed {
msg = "failed"
statusCode = http.StatusInternalServerError
}
fullBody := map[string]interface{}{
"status": msg,
"details": states,
}
for k, v := range custom {
if k != "status" && k != "details" {
fullBody[k] = v
}
}
data, err := json.Marshal(fullBody)
if err != nil {
writeJSONStatus(rw, "error", fmt.Sprintf("Failed to marshal state data: %v", err), http.StatusOK)
return
}
writeJSONResponse(rw, statusCode, data)
})
}
func writeJSONStatus(rw http.ResponseWriter, status, message string, statusCode int) {
jsonData, _ := json.Marshal(&jsonStatus{
Message: message,
Status: status,
})
writeJSONResponse(rw, statusCode, jsonData)
}
func writeJSONResponse(rw http.ResponseWriter, statusCode int, content []byte) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(statusCode)
rw.Write(content)
}