-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.go
48 lines (41 loc) · 1.3 KB
/
check.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
package rest
import (
"encoding/json"
"net/http"
)
// HealthStatus represents the result of a health check
type HealthStatus string
const (
// HealthStatusUnknown indicates that the health status was not reported
HealthStatusUnknown HealthStatus = ""
// HealthStatusOK indicates that the server reported the status to be ok
HealthStatusOK HealthStatus = "ok"
// HealthStatusDegraded indicates that the server reported the status
// to be degraded
HealthStatusDegraded HealthStatus = "degraded"
)
// HealthCheckResponse encapsulates the response from the server health check
type HealthCheckResponse struct {
Status HealthStatus `json:"status"`
}
// HealthCheck requests the health of the rest server
func (host Host) HealthCheck() (HealthStatus, error) {
var checkStatus HealthCheckResponse
uri := host.uri + healthCheckSubPath
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return HealthStatusUnknown, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := host.client.Do(req)
if err != nil {
// should report auth problems here in future
return HealthStatusUnknown, err
}
defer resp.Body.Close()
if err := DecodeOCError(resp); err != nil {
return HealthStatusUnknown, err
}
err = json.NewDecoder(resp.Body).Decode(&checkStatus)
return checkStatus.Status, err
}