-
Notifications
You must be signed in to change notification settings - Fork 133
/
http_handler.go
36 lines (31 loc) · 940 Bytes
/
http_handler.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
package health
import (
"net/http"
log "github.com/sirupsen/logrus"
)
// TODO Doesn't need to exist. Just give a Checker directly.
type CheckHttpHandler struct {
checker Checker
}
func NewCheckHttpHandler(checker Checker) *CheckHttpHandler {
return &CheckHttpHandler{
checker: checker,
}
}
// TODO Is this really the way to do it? We could give a handler that is an anonymous function
// handle that encloses the relevant variables.
// In doing so, we don't need CheckHttpHandler, NewCheckHttpHandler.
func (h *CheckHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h.checker.Check()
if err == nil {
log.Info("Health check passed")
w.WriteHeader(http.StatusNoContent)
} else {
log.Warnf("Health check failed: %v", err)
w.WriteHeader(http.StatusServiceUnavailable)
_, err = w.Write([]byte(err.Error()))
if err != nil {
log.Errorf("Failed to write health check response: %v", err)
}
}
}