-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.go
56 lines (45 loc) · 1.18 KB
/
checker.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
package healthchecks
import (
"net/http"
)
const okMessage = "OK"
const notOkMessage = "NOT OK"
// Checker exposes an interface for k8s monitoring/health checks
type Checker interface {
HealthHandlerFunc(w http.ResponseWriter, r *http.Request)
ReadyHandlerFunc(w http.ResponseWriter, r *http.Request)
SetReady(ready bool) bool
SetHealthy(health bool) bool
}
type checker struct {
healthy bool
ready bool
}
//New returns a concrete implementation of the checker
func New() Checker {
return &checker{false, false}
}
func (c *checker) SetHealthy(healthy bool) bool {
c.healthy = healthy
return healthy
}
func (c *checker) HealthHandlerFunc(w http.ResponseWriter, r *http.Request) {
c.handlerFunc(c.healthy, w, r)
}
func (c *checker) SetReady(ready bool) bool {
c.ready = ready
return ready
}
func (c *checker) ReadyHandlerFunc(w http.ResponseWriter, r *http.Request) {
c.handlerFunc(c.ready, w, r)
}
func (c *checker) handlerFunc(state bool, w http.ResponseWriter, r *http.Request) {
message := okMessage
if !state {
message = notOkMessage
w.WriteHeader(http.StatusInternalServerError)
}
bytes := []byte(message)
w.Header().Set("Content-Type", "text/plain")
w.Write(bytes)
}