/
healthcheck.go
92 lines (74 loc) · 1.91 KB
/
healthcheck.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
package controllers
import (
"encoding/json"
"net/http"
"flamingo.me/flamingo/v3/core/healthcheck/domain/healthcheck"
)
type (
statusProvider func() map[string]healthcheck.Status
// Healthcheck controller
Healthcheck struct {
statusProvider statusProvider
}
// Ping controller
Ping struct{}
response struct {
Services []service `json:"services,omitempty"`
}
service struct {
Name string `json:"name"`
Alive bool `json:"alive"`
Details string `json:"details"`
}
)
// Inject Healthcheck dependencies
func (h *Healthcheck) Inject(provider statusProvider) {
h.statusProvider = provider
}
// ServeHTTP responds to healthcheck requests
func (h *Healthcheck) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
var resp response
var allAlive = true
for name, status := range h.statusProvider() {
alive, details := status.Status()
if !alive {
allAlive = false
}
resp.Services = append(resp.Services, service{
Name: name,
Alive: alive,
Details: details,
})
}
var status = http.StatusOK
if !allAlive {
status = http.StatusInternalServerError
}
respBody, err := json.Marshal(resp)
handleErr(err, w)
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_, err = w.Write(respBody)
handleErr(err, w)
}
// ServeHTTP responds to Ping requests
func (p *Ping) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("OK"))
handleErr(err, w)
}
// TryServeHTTP implementation to be used in prefixrouter & co
func (p *Ping) TryServeHTTP(rw http.ResponseWriter, req *http.Request) (bool, error) {
if req.URL.Path != "/health/ping" {
return true, nil
}
rw.WriteHeader(http.StatusOK)
_, err := rw.Write([]byte("OK"))
return false, err
}
func handleErr(err error, w http.ResponseWriter) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
}
}