-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhealth_check_server.go
46 lines (39 loc) · 947 Bytes
/
health_check_server.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
package main
import "io"
import "net/http"
import "os"
type healthCheckServer struct {
HealthyIfFile string
HealthyUnlessFile string
}
func HealthCheckServer(healthyIfFile string, healthyUnlessFile string) healthCheckServer {
return healthCheckServer{
HealthyIfFile: AddRoot(healthyIfFile),
HealthyUnlessFile: AddRoot(healthyUnlessFile),
}
}
func AddRoot(path string) string {
if len(path) > 0 && path[0] != '/' {
return "/" + path
} else {
return path
}
}
func (server healthCheckServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if server.HealthyIfFile != "" {
_, err := os.Stat(server.HealthyIfFile)
if os.IsNotExist(err) {
http.Error(w, "Offline", 503)
return
}
}
if server.HealthyUnlessFile != "" {
_, err := os.Stat(server.HealthyUnlessFile)
if !os.IsNotExist(err) {
http.Error(w, "Offline", 503)
return
}
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, "Online\n")
}