-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (144 loc) · 3.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/golang/glog"
)
// Ready represent the readiness from the host
type Ready struct {
Database string `json:"database"`
ExternalAccess string `json:"external_access"`
}
var (
c Config
readiness = false
)
func message(w http.ResponseWriter, r *http.Request, c *Config) {
m := builder(c)
w.Header().Set("Content-Type", "application/json")
glog.V(3).Infoln("Encoding message to Json")
if err := json.NewEncoder(w).Encode(m); err != nil {
panic(err)
}
}
func service(w http.ResponseWriter, r *http.Request) {
if readiness {
message(w, r, &c)
} else {
glog.V(3).Infof("Endpoint is waiting for Readiness")
w.WriteHeader(http.StatusServiceUnavailable)
}
}
func fail(w http.ResponseWriter, r *http.Request) {
readiness = false
host, _ := os.Hostname()
glog.V(3).Infof("Locking service at %s", host)
}
func live(w http.ResponseWriter, r *http.Request) {
hc := r.Header.Get("HEALTHCHECK")
host, _ := os.Hostname()
if readiness {
glog.V(3).Infof("Liveness probe is alive at %s from %s", host, hc)
w.WriteHeader(http.StatusOK)
} else {
glog.V(3).Infof("Liveness probe is waiting for Readiness at %s from %s", host, hc)
w.WriteHeader(http.StatusServiceUnavailable)
}
}
func ready(w http.ResponseWriter, r *http.Request) {
hc := r.Header.Get("HEALTHCHECK")
host, _ := os.Hostname()
var status Ready
if readiness {
status = Ready{
"Ready",
"Ready",
}
w.WriteHeader(http.StatusOK)
glog.V(3).Infof("everything is going well at %s from %s", host, hc)
} else {
status = Ready{
"Unknow",
"Unknow",
}
glog.V(3).Infof("Readiness is working at %s from %s", host, hc)
w.WriteHeader(http.StatusServiceUnavailable)
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(status); err != nil {
panic(err)
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s -readinessStart 30 -config config.yml -logtostderr -v=3\n\n", os.Args[0])
flag.PrintDefaults()
}
version := flag.Bool("version", false, "Print the version information")
cFile := flag.String("config", "", "Configuration file")
rStart := flag.Int("readinessStart", 10, "Readiness start seconds")
lReload := flag.Int("livenessReload", 10, "Liveness reload seconds")
lEnds := flag.Int("livenessEnds", 10, "Liveness ends seconds")
flag.Parse()
if *version {
fmt.Println(NewInfo().Print())
os.Exit(0)
}
p := os.Getenv("PORT")
if p == "" {
p = "9000"
}
e := os.Getenv("EXTERNAL")
if *cFile == "" && e != "true" {
flag.Usage()
os.Exit(1)
}
glog.V(3).Infoln("Flags")
glog.V(3).Infof("\tconfig : %v", *cFile)
glog.V(3).Infof("\treadinessStart %v seconds", *rStart)
glog.V(3).Infof("\tlivenessReload %v seconds", *lReload)
glog.V(3).Infof("\tlivenessEnds %v seconds", *lEnds)
_, err := c.Read(*cFile)
if err != nil {
glog.Fatal(err)
}
go func() {
time.Sleep(time.Second * time.Duration(*rStart))
readiness = true
}()
srv := &http.Server{Addr: fmt.Sprintf(":%s", p)}
http.HandleFunc("/", service)
http.HandleFunc("/live", live)
http.HandleFunc("/ready", ready)
http.HandleFunc("/fail", fail)
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
glog.V(1).Infof("Running on http://localhost:%s", p)
if err := srv.ListenAndServe(); err != nil {
glog.V(4).Infof("HTTP server ListenAndServe: %v", err)
}
}()
<-signals
glog.V(4).Infoln("Stopping server gracefully")
graceTimeOut := 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), graceTimeOut)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
if ctx.Err() == context.DeadlineExceeded {
glog.V(4).Infof("Wait server shutdown is over due to: %s", err)
err = srv.Close()
if err != nil {
glog.Error(err)
}
}
}
}