-
Notifications
You must be signed in to change notification settings - Fork 684
/
health_check_server.go
135 lines (113 loc) · 3.35 KB
/
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
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
package services
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/datawire/dlib/dgroup"
"github.com/datawire/dlib/dhttp"
"github.com/datawire/dlib/dlog"
)
// HTTP server object (all fields are required).
type HealthCheckServer struct {
Port int16
Backend string
SecurePort int16
SecureBackend string
Cert string
Key string
TLSVersion string
Healthy bool
HealthyStatusCode int
UnhealthyStatusCode int
}
// Start initializes the Health Check HTTP server.
func (h *HealthCheckServer) Start(ctx context.Context) <-chan bool {
dlog.Printf(ctx, "HTTP: %s listening on %d/%d", h.Backend, h.Port, h.SecurePort)
h.Healthy = true
mux := http.NewServeMux()
mux.HandleFunc("/", h.handler)
sc := &dhttp.ServerConfig{
Handler: mux,
}
g := dgroup.NewGroup(ctx, dgroup.GroupConfig{})
g.Go("cleartext", func(ctx context.Context) error {
return sc.ListenAndServe(ctx, fmt.Sprintf(":%v", h.Port))
})
g.Go("tls", func(ctx context.Context) error {
return sc.ListenAndServeTLS(ctx, fmt.Sprintf(":%v", h.SecurePort), h.Cert, h.Key)
})
exited := make(chan bool)
go func() {
if err := g.Wait(); err != nil {
dlog.Error(ctx, err)
panic(err) // TODO: do something better
}
close(exited)
}()
return exited
}
func (h *HealthCheckServer) handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Assume we're the clear side of the world.
backend := h.Backend
conntype := "CLR"
var request = make(map[string]interface{})
var url = make(map[string]interface{})
request["url"] = url
url["fragment"] = r.URL.Fragment
url["host"] = r.URL.Host
url["opaque"] = r.URL.Opaque
url["path"] = r.URL.Path
url["query"] = r.URL.Query()
url["rawQuery"] = r.URL.RawQuery
url["scheme"] = r.URL.Scheme
if r.URL.User != nil {
url["username"] = r.URL.User.Username()
pw, ok := r.URL.User.Password()
if ok {
url["password"] = pw
}
}
request["method"] = r.Method
request["headers"] = lower(r.Header)
request["host"] = r.Host
var tlsrequest = make(map[string]interface{})
request["tls"] = tlsrequest
tlsrequest["enabled"] = r.TLS != nil
if r.TLS != nil {
// We're the secure side of the world, I guess.
backend = h.SecureBackend
conntype = "TLS"
tlsrequest["negotiated-protocol"] = r.TLS.NegotiatedProtocol
tlsrequest["server-name"] = r.TLS.ServerName
tlsrequest["negotiated-protocol-version"] = getTLSVersion(r.TLS)
}
// Set date response header.
w.Header().Set("Date", time.Now().Format(time.RFC1123))
statusCode := h.HealthyStatusCode
if !h.Healthy {
statusCode = h.UnhealthyStatusCode
}
fmt.Println(r.URL.Path)
// A request to this path will make the health check server respond with
// only the UnhealthyStatusCode to all subsequent requests
if r.URL.Path == "/makeUnhealthy/" {
h.Healthy = false
}
w.WriteHeader(statusCode)
// Write out all request/response information
var response = make(map[string]interface{})
response["headers"] = lower(w.Header())
var body = make(map[string]interface{})
body["backend"] = backend
body["request"] = request
body["response"] = response
b, err := json.MarshalIndent(body, "", " ")
if err != nil {
b = []byte(fmt.Sprintf("Error: %v", err))
}
dlog.Printf(ctx, "%s (%s): \"%s %s\" -> HTTP %v", r.Method, r.URL.Path, backend, conntype, statusCode)
_, _ = w.Write(b)
}