-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfo.go
73 lines (64 loc) · 1.75 KB
/
info.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
package libhealth
import (
"bytes"
"encoding/json"
"net/http"
"strings"
)
// Paths to info healthchecks.
const (
InfoHealthCheck = `/info/healthcheck`
InfoHealthCheckLive = `/info/healthcheck/live`
infoBad = `{"condition":"info healthcheck error"}`
)
// Info is an http.Handler for
//
// /info/healthcheck
// /info/healthcheck/live.
type Info struct {
deps DependencySet
}
// NewInfo will create a new Info handler for a given DependencySet set.
func NewInfo(d DependencySet) *Info {
return &Info{d}
}
// InfoResult represents the body of an info healthcheck.
type InfoResult struct {
Condition string `json:"condition"`
Hostname string `json:"hostname"`
Duration int64 `json:"duration"`
}
func (i *Info) generate(live bool, hostname string) ([]byte, int) {
var s Summary
if live {
s = i.deps.Live()
} else {
s = i.deps.Background()
}
r := InfoResult{
Condition: s.Overall().String(),
Hostname: hostname,
Duration: int64(s.Duration()) / 1000000,
}
bytes, err := json.Marshal(r)
if err != nil {
return []byte(infoBad), http.StatusInternalServerError
}
return bytes, ComputeStatusCode(true, s)
}
// ServeHTTP is intended to be used by a net/http.ServeMux for serving formatted json.
func (i *Info) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
live := strings.HasSuffix(r.URL.String(), "/live")
healthcheckJSON, code := i.generate(live, hostname())
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, healthcheckJSON, "", " "); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(code)
if _, err := w.Write(prettyJSON.Bytes()); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}