forked from Azure/application-gateway-kubernetes-ingress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
36 lines (29 loc) · 1.09 KB
/
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
// -------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// --------------------------------------------------------------------------------------------
package health
import "net/http"
// Probe is a type alias for a function.
type Probe func() bool
// Probes is the interface for liveness and readiness probes
type Probes interface {
Liveness() bool
Readiness() bool
}
func makeHandler(probe Probe) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(map[bool]int{
true: http.StatusOK,
false: http.StatusServiceUnavailable,
}[probe()])
})
}
// ReadinessHandler returns readiness http handlers for health
func ReadinessHandler(probe Probes) http.Handler {
return makeHandler(probe.Readiness)
}
// LivenessHandler returns readiness http handlers for health
func LivenessHandler(probe Probes) http.Handler {
return makeHandler(probe.Liveness)
}