forked from coredns/coredns
/
health.go
57 lines (47 loc) · 787 Bytes
/
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
package health
import (
"io"
"log"
"net"
"net/http"
"sync"
)
var once sync.Once
type Health struct {
Addr string
ln net.Listener
mux *http.ServeMux
}
func health(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, ok)
}
func (h *Health) Startup() error {
if h.Addr == "" {
h.Addr = defAddr
}
once.Do(func() {
if ln, err := net.Listen("tcp", h.Addr); err != nil {
log.Printf("[ERROR] Failed to start health handler: %s", err)
return
} else {
h.ln = ln
}
h.mux = http.NewServeMux()
h.mux.HandleFunc(path, health)
go func() {
http.Serve(h.ln, h.mux)
}()
})
return nil
}
func (h *Health) Shutdown() error {
if h.ln != nil {
return h.ln.Close()
}
return nil
}
const (
ok = "OK"
defAddr = ":8080"
path = "/health"
)