-
Notifications
You must be signed in to change notification settings - Fork 126
/
health.go
50 lines (36 loc) · 1 KB
/
health.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
package service
import (
"context"
"net/http"
)
const (
serviceUp = "UP"
serviceDown = "DOWN"
)
type Health struct {
Status string `json:"status"`
Details map[string]interface{} `json:"details"`
}
func (h *httpService) HealthCheck(ctx context.Context) *Health {
return h.getHealthResponseForEndpoint(ctx, ".well-known/alive")
}
func (h *httpService) getHealthResponseForEndpoint(ctx context.Context, endpoint string) *Health {
var healthResponse = Health{
Details: make(map[string]interface{}),
}
resp, err := h.Get(ctx, endpoint, nil)
if err != nil || resp == nil {
healthResponse.Status = serviceDown
healthResponse.Details["error"] = err.Error()
return &healthResponse
}
defer resp.Body.Close()
healthResponse.Details["host"] = resp.Request.URL.Host
if resp.StatusCode == http.StatusOK {
healthResponse.Status = serviceUp
return &healthResponse
}
healthResponse.Status = serviceDown
healthResponse.Details["error"] = "service down"
return &healthResponse
}