-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
224 lines (190 loc) · 5.21 KB
/
web.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"context"
"encoding/json"
"html/template"
"net/http"
"strings"
"sync/atomic"
"time"
packr "github.com/gobuffalo/packr/v2"
consul "github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
"github.com/segmentio/ksuid"
)
type serverShutdownError struct {
st hclog.CapturedStacktrace
err error
}
type key int
const (
requestIDKey key = iota
)
type certData struct {
FQDN string
ExtraNames []string
NotAfter time.Time
}
type indexData struct {
Version, GitSHA string
Certs []certData
}
var (
templatesBox, assetsBox *packr.Box
templates map[string]*template.Template
healthy int32
)
func init() {
healthy = 1
templatesBox = packr.New("Templates", "./web/templates")
assetsBox = packr.New("Assets", "./web/assets")
templates = make(map[string]*template.Template)
}
func runWeb(ctx context.Context, bind, prefix string, kv *consul.KV) error {
done := make(chan serverShutdownError, 1)
logger := baseLogger.Named("Web")
logger.Info("Web listener starting", "bind", bind)
mux := http.NewServeMux()
mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(assetsBox)))
mux.Handle("/healthz", handleHealthz(prefix, kv))
mux.Handle("/", handleIndex(prefix, kv))
nextRequestID := func() string { return ksuid.New().String() }
server := &http.Server{
Addr: bind,
Handler: tracing(nextRequestID)(logging(logger)(mux)),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
go func() {
<-ctx.Done()
logger.Debug("Shutting down webserver")
atomic.StoreInt32(&healthy, 0)
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(shutdownCtx); err != nil {
st := hclog.Stacktrace()
done <- serverShutdownError{st, err}
}
close(done)
}()
server.ListenAndServe()
shutdownErr, ok := <-done
if ok {
logger.Error("Unable to gracefully shutdown server", "err", shutdownErr.err, shutdownErr.st)
return shutdownErr.err
}
return nil
}
func handleIndex(prefix string, kv *consul.KV) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
certs, err := getAllCerts(req.Context(), kv, prefix)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
data := &indexData{
Version: Version,
GitSHA: GitSHA,
}
data.Certs = make([]certData, len(certs))
for i, cert := range certs {
names := make([]string, 0)
for _, name := range cert.DNSNames {
if name == cert.Subject.CommonName {
continue
}
names = append(names, name)
}
data.Certs[i] = certData{
FQDN: cert.Subject.CommonName,
ExtraNames: names,
NotAfter: cert.NotAfter,
}
}
err = renderTemplate("index.gohtml", data, w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
})
}
func handleHealthz(prefix string, kv *consul.KV) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if atomic.LoadInt32(&healthy) != 1 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
status := map[string]interface{}{
"watchers": watchers,
}
err := renderJSON(status, w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
})
}
func renderTemplate(name string, data interface{}, w http.ResponseWriter) error {
var (
tmpl *template.Template
ok bool
)
tmpl, ok = templates[name]
if !ok {
var file string
file, err := templatesBox.FindString(name)
if err != nil {
return err
}
tmpl, err = template.New(name).Funcs(template.FuncMap{"StringsJoin": strings.Join}).Parse(file)
if err != nil {
return err
}
templates[name] = tmpl
}
return tmpl.Execute(w, data)
}
func renderJSON(data interface{}, w http.ResponseWriter) error {
out, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
return err
}
// Compose function for a ServerMux (or other http.Handler) to add request logging
func logging(logger hclog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Info(requestID, "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr, "userAgent", r.UserAgent())
}()
next.ServeHTTP(w, r)
})
}
}
// Compose function for a ServerMux (or other http.Handler) to add request ID tagging
func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}