This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
72 lines (62 loc) · 1.86 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sdk
import (
"encoding/json"
"fmt"
"net/http"
)
type HealthStatus uint
const (
StatusUnknown HealthStatus = iota //Default return value if a request error occurs
StatusHealthy // All systems are in a operational state
StatusPartiallyDegraded // Some systems are degraded, for example one out of more Hardware nodes have failed recently
StatusDegraded // The database connection has failed, Smarthome cannot be used in this state
)
type VersionResponse struct {
Version string `json:"version"`
GoVersion string `json:"goVersion"`
}
// Can be used in order to check if the Smarthome server is reachable and responds
// Returns an ENUM type indicating the overall health status of the server
/** Errors
- nil
- ErrConnFailed
- ErrReadResponseBody
*/
func (c *Connection) HealthCheck() (status HealthStatus, err error) {
u := c.SmarthomeURL
u.Path = "/health"
// Check if the base URL is working and the server is reachable
res, err := http.Get(u.String())
if err != nil {
return StatusUnknown, ErrConnFailed
}
switch res.StatusCode {
case 200:
return StatusHealthy, nil
case 502:
return StatusPartiallyDegraded, nil
case 503:
return StatusDegraded, nil
}
return StatusUnknown, fmt.Errorf("unknown response code: %s", res.Status)
}
// Can be used to retrieve the current version of the Smarthome server
/** Errors
- nil
- ErrConnFailed
- ErrReadResponseBody
*/
func (c *Connection) Version() (version VersionResponse, err error) {
u := c.SmarthomeURL
u.Path = "/api/version"
res, err := http.Get(u.String())
if err != nil {
return VersionResponse{}, ErrConnFailed
}
decoder := json.NewDecoder(res.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&version); err != nil {
return VersionResponse{}, ErrReadResponseBody
}
return version, nil
}