This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
/
handler_status.go
83 lines (72 loc) · 1.79 KB
/
handler_status.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
package main
import (
"encoding/json"
"net/http"
consul "github.com/hashicorp/consul/api"
nomad "github.com/hashicorp/nomad/api"
"github.com/jippi/hashi-ui/backend/config"
)
// StatusHandler establishes the websocket connection and calls the connection handler.
func StatusHandler(cfg *config.Config, nomadClient *nomad.Client, consulClient *consul.Client) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var healthy *bool
status := struct {
Healty *bool
Consul map[string]interface{}
Nomad map[string]interface{}
}{
Consul: make(map[string]interface{}),
Nomad: make(map[string]interface{}),
}
healthy = nil
if nomadClient != nil {
leader, err := nomadClient.Status().Leader()
status.Nomad["enabled"] = true
status.Nomad["status"] = struct {
Leader string
Error error
}{leader, err}
if err != nil {
healthy = newBool(false)
} else {
healthy = newBool(true)
}
} else {
status.Nomad["enabled"] = false
}
if consulClient != nil {
leader, err := consulClient.Status().Leader()
status.Consul["enabled"] = true
status.Consul["status"] = struct {
Leader string
Error error
}{leader, err}
if err != nil {
healthy = newBool(false)
} else {
if healthy != nil {
healthy = newBool(*healthy && true)
} else {
healthy = newBool(true)
}
}
} else {
status.Consul["enabled"] = false
}
if healthy != nil {
status.Healty = healthy
} else {
status.Healty = newBool(false)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if *healthy {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
json.NewEncoder(w).Encode(status)
}
}
func newBool(b bool) *bool {
return &b
}