-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
53 lines (43 loc) · 1.19 KB
/
server.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
package gossboss
import (
"encoding/json"
"log"
"net/http"
)
// Server is a gossboss server.
type Server struct {
GossServers []string
Port string
Client *Client
}
// NewServer returns a new Server.
func NewServer(port string, gossServers []string) *Server {
return &Server{
Port: port,
GossServers: gossServers,
Client: NewClient(),
}
}
// Serve establishes a gossboss server.
func (s *Server) Serve() {
http.HandleFunc("/healthzs", s.HandleHealthzs)
log.Println("Starting server on", s.Port)
if err := http.ListenAndServe(s.Port, nil); err != nil {
log.Fatal(err)
}
}
// HandleHealthzs collects the /healthz responses from all the GossServers
// and returns a JSON array of their responses.
func (s *Server) HandleHealthzs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
hzs := s.Client.CollectHealthzs(s.GossServers)
responseCode := http.StatusOK
if hzs.Summary.Failed != 0 || hzs.Summary.Errored != 0 {
responseCode = http.StatusInternalServerError
}
w.WriteHeader(responseCode)
err := json.NewEncoder(w).Encode(hzs)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}