-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 1.33 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
var ok bool
var (
Term os.Signal = syscall.SIGTERM
)
// log the Signal to STDOUT
func logSignal(c chan os.Signal) {
for s := range c {
log.Println("Got signal:", s)
ok = false
}
}
func main() {
ok = true
c := make(chan os.Signal)
signal.Notify(c, Term)
go logSignal(c)
log.Println("starting server at :8080")
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if ok {
log.Printf("/healthz - OK - %d", http.StatusOK)
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
} else {
log.Printf("/healthz - FAIL - %d", http.StatusServiceUnavailable)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("FAIL"))
return
}
})
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
if ok {
log.Printf("/ready - OK - %d", http.StatusOK)
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
} else {
log.Printf("/ready - FAIL - %d", http.StatusServiceUnavailable)
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("FAIL"))
return
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("/- OK - %d", http.StatusOK)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Welcome!\n"))
})
http.ListenAndServe(":8080", nil)
}