/
service_healthcheck.go
101 lines (85 loc) · 2.29 KB
/
service_healthcheck.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package adminapi
import (
"github.com/HydroProtocol/hydro-sdk-backend/utils"
"net"
"net/http"
"os"
"time"
)
const (
ServiceOnline = "online"
ServiceOffline = "offline"
)
//return status of services
type IHealthCheckMonitor interface {
CheckWeb() string
CheckApi() string
CheckEngine() string
CheckLauncher() string
CheckWatcher() string
CheckWebSocket() string
}
type HealthCheckService struct {
client utils.IHttpClient
options *HealthCheckOptions
}
type HealthCheckOptions struct {
webUrl string
apiUrl string
engineUrl string
launcherUrl string
watcherUrl string
websocketUrl string
}
func NewHealthCheckService(options *HealthCheckOptions) IHealthCheckMonitor {
if options == nil {
options = &HealthCheckOptions{
webUrl: os.Getenv("WEB_HEALTH_CHECK_URL"),
apiUrl: os.Getenv("API_HEALTH_CHECK_URL"),
engineUrl: os.Getenv("ENGINE_HEALTH_CHECK_URL"),
launcherUrl: os.Getenv("LAUNCHER_HEALTH_CHECK_URL"),
watcherUrl: os.Getenv("WATCHER_HEALTH_CHECK_URL"),
websocketUrl: os.Getenv("WEBSOCKET_HEALTH_CHECK_URL"),
}
}
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 500 * time.Millisecond,
}).DialContext,
TLSHandshakeTimeout: 1000 * time.Millisecond,
}
return &HealthCheckService{
client: utils.NewHttpClient(transport),
options: options,
}
}
func (h *HealthCheckService) CheckWeb() string {
_, code, _ := h.client.Get(h.options.webUrl, nil, nil, nil)
return ToStatus(code)
}
func (h *HealthCheckService) CheckApi() string {
_, code, _ := h.client.Get(h.options.apiUrl, nil, nil, nil)
return ToStatus(code)
}
func (h *HealthCheckService) CheckEngine() string {
_, code, _ := h.client.Get(h.options.engineUrl, nil, nil, nil)
return ToStatus(code)
}
func (h *HealthCheckService) CheckLauncher() string {
_, code, _ := h.client.Get(h.options.launcherUrl, nil, nil, nil)
return ToStatus(code)
}
func (h *HealthCheckService) CheckWatcher() string {
_, code, _ := h.client.Get(h.options.watcherUrl, nil, nil, nil)
return ToStatus(code)
}
func (h *HealthCheckService) CheckWebSocket() string {
_, code, _ := h.client.Get(h.options.websocketUrl, nil, nil, nil)
return ToStatus(code)
}
func ToStatus(httpCode int) string {
if http.StatusOK == httpCode {
return ServiceOnline
}
return ServiceOffline
}