-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux.go
55 lines (44 loc) · 1.18 KB
/
mux.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 talon_access_proxy
import (
"io"
"net/http"
"go.uber.org/zap"
)
type mux struct {
Tap *Tap
Logger *zap.Logger
}
func newMux(t *Tap) *mux {
return &mux{
Tap: t,
Logger: t.Config.Logger.With(zap.String("tag", "Mux")),
}
}
func (mux *mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.Logger.Debug("Got Request", zap.String("method", r.Method), zap.String("url", r.URL.String()), zap.Int64("content-length", r.ContentLength), zap.Any("headers", r.Header))
if r.URL.String() == "/.health" {
w.WriteHeader(http.StatusOK)
return
}
response, err := mux.Tap.doHTTPRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// copy all headers
for header, value := range response.Header {
w.Header().Set(header, value[0])
}
w.Header().Set("X-TAP", Version)
mux.Logger.Debug("Sending Response",
zap.Int64("content-length", response.ContentLength),
zap.Any("headers", w.Header()))
w.WriteHeader(response.StatusCode)
if response.Body != nil {
defer response.Body.Close()
_, err := io.Copy(w, response.Body)
if err != nil && err != io.EOF {
mux.Logger.Error("Unable to copy body", zap.Error(err))
}
}
}