-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhealthz.go
45 lines (40 loc) · 967 Bytes
/
healthz.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
package main
import (
"encoding/json"
"net/http"
)
const (
healthzPath = "healthz"
)
// json sent back on /status requests
type HealthzResponse struct {
NbOpenHooks int `json:"nb_open_hooks"`
}
// healthzServe returns infos on running server
// currently the number of currently open hooks
func healthzServe(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
s := &HealthzResponse{
NbOpenHooks: sessionsCount(),
}
if err := json.NewEncoder(w).Encode(s); err != nil {
panic(err)
}
}
// getHealthz queries the /status endpoint (used by tests)
func getHealthz(url string) (*HealthzResponse, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, err
}
var sr HealthzResponse
if err := json.NewDecoder(res.Body).Decode(&sr); err != nil {
return nil, err
}
return &sr, nil
}