-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (46 loc) · 1.11 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
)
var (
healthy int32 = 1 // 1 = 200 OK, 0 = 410 Gone
port = flag.Int("p", 8000, "port to listen on") // port as flag
)
func healthzHandler(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 1 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 OK"))
} else {
w.WriteHeader(http.StatusGone)
w.Write([]byte("410 GONE"))
}
}
func stopHandler(w http.ResponseWriter, r *http.Request) {
atomic.StoreInt32(&healthy, 0)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Switched to 410 GONE mode"))
}
func main() {
// Parsing command-line flags
flag.Parse()
http.HandleFunc("/healthz", healthzHandler)
http.HandleFunc("/stop", stopHandler)
// Signal handling for SIGTERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
go func() {
<-sigs
atomic.StoreInt32(&healthy, 0)
}()
address := fmt.Sprintf(":%d", *port)
fmt.Printf("Starting server on port %d...\n", *port)
if err := http.ListenAndServe(address, nil); err != nil {
fmt.Println("Server error:", err)
}
}