forked from coredns/coredns
/
health.go
59 lines (47 loc) · 845 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
58
59
// Package health implements an HTTP handler that responds to health checks.
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 (h *health) Startup() error {
if h.Addr == "" {
h.Addr = defAddr
}
once.Do(func() {
ln, err := net.Listen("tcp", h.Addr)
if err != nil {
log.Printf("[ERROR] Failed to start health handler: %s", err)
return
}
h.ln = ln
h.mux = http.NewServeMux()
h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, ok)
})
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"
)