-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
58 lines (48 loc) · 1.02 KB
/
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
package agent
import (
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
)
// StatusServer is used to query the agent status from the outside
type StatusServer interface {
Run() error
}
// NewStatusServer creates a new StatusServer
func NewStatusServer(readyCh <-chan interface{}) StatusServer {
s := &statusServer{
log: log.WithField("module", "StatusServer"),
}
mux := http.NewServeMux()
mux.HandleFunc("/ready", s.readyHandler)
server := &http.Server{
Addr: "0.0.0.0:9000",
Handler: mux,
}
s.server = server
go func() {
<-readyCh
s.log.Info("Status changed to ready")
s.ready = true
}()
return s
}
type statusServer struct {
ready bool
server *http.Server
log *log.Entry
}
func (s *statusServer) Run() error {
err := s.server.ListenAndServe()
if err != http.ErrServerClosed {
return err
}
return nil
}
func (s *statusServer) readyHandler(w http.ResponseWriter, r *http.Request) {
if s.ready {
fmt.Fprint(w, "ready")
return
}
http.Error(w, "not ready", http.StatusServiceUnavailable)
}