-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.go
45 lines (40 loc) · 1.06 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
package checker
import "net/http"
type ckeckFunc func() error
// Creates a simple HTTP server listening on port 8080 with only GET:/live and
// GET:/ready endpoints. It takes liveness and readiness functions as
// parameters. Each parameter is a ckeckFunc function. It simply returns an
// error if the probe fails.
//
// Example:
//
// // Use a separate coroutine
// go checker.HTTP(
// func() error {
// if !app.Ready() {
// return errors.New("App is not ready")
// }
//
// return nil
// },
// func() error {
// if !app.Live() {
// return errors.New("App is not live")
// }
//
// return nil
// },
// )
func HTTP(liveness, readiness ckeckFunc) error {
http.HandleFunc("/live", handleHTTPCheck(liveness))
http.HandleFunc("/ready", handleHTTPCheck(readiness))
return http.ListenAndServe(":8080", nil)
}
func handleHTTPCheck(c ckeckFunc) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
if err := c(); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
}
}