/
healthchecks.go
50 lines (44 loc) · 1.16 KB
/
healthchecks.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
package healthchecks
import (
"fmt"
"net/http"
"github.com/18F/cf-domain-broker-alb/config"
)
var checks = map[string]func(config.Settings) error{
"letsencrypt": LetsEncrypt,
"s3": S3,
"alb": ALB,
"cloudfoundry": Cloudfoundry,
"postgresql": Postgresql,
}
func Bind(mux *http.ServeMux, settings config.Settings) {
mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
body := ""
for name, function := range checks {
err := function(settings)
if err != nil {
body = body + fmt.Sprintf("%s error: %s\n", name, err)
}
}
if body != "" {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", body)
} else {
w.WriteHeader(http.StatusOK)
}
})
mux.HandleFunc("/healthcheck/http", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
for name, function := range checks {
mux.HandleFunc("/healthcheck/"+name, func(w http.ResponseWriter, r *http.Request) {
err := function(settings)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s error: %s", name, err)
} else {
w.WriteHeader(http.StatusOK)
}
})
}
}