-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
103 lines (85 loc) · 2.8 KB
/
middleware.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
package main
import (
"context"
"net/http"
"strconv"
"sync/atomic"
"time"
"github.com/gorilla/handlers"
)
// middleware is a set of common middleware handlers for all requests.
func (s *server) middleware(next http.Handler) http.Handler {
return s.mwReqID(s.mwRecover(s.mwProxy(s.mwLog(s.mwServerHeader(s.mwCompress(next))))))
}
// mwCompress will compress responses.
func (s *server) mwCompress(next http.Handler) http.Handler {
return handlers.CompressHandler(next)
}
// mwProxy pulls proxy headers from the request.
func (s *server) mwProxy(next http.Handler) http.Handler {
return handlers.ProxyHeaders(next)
}
// mwServerHeader adds a Server header to responses.
func (s *server) mwServerHeader(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", appName+"/"+appVersion)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// mwRecover recovers from a panic that occurred when handling a request.
func (s *server) mwRecover(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
s.Log(LevelError, r.Context(), "panic serving request: %v", err)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
type reqIDKey struct{}
// mwReqID assigns a unique ID to each HTTP request.
func (s *server) mwReqID(next http.Handler) http.Handler {
// gReqID is an atomically incremented variable to identify each HTTP request with a unique ID.
var gReqID uint64
fn := func(w http.ResponseWriter, r *http.Request) {
// increment request sequence
reqID := strconv.FormatUint(atomic.AddUint64(&gReqID, 1), 10)
r = r.WithContext(context.WithValue(r.Context(), reqIDKey{}, reqID))
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// mwLog logs a request, response, and timing information.
func (s *server) mwLog(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
// log incoming request data
s.Log(LevelInfo, req.Context(), "<-- %s %s from %s", req.Method, req.RequestURI, req.RemoteAddr)
rsp := &responseLogger{ResponseWriter: w}
// pass through to next handler
next.ServeHTTP(rsp, req)
var level = LevelInfo
if rsp.status >= 500 {
level = LevelError
} else if rsp.status >= 400 {
level = LevelWarn
}
// log final request data
s.Log(level, req.Context(), "--> %d (%s) in %s", rsp.status, http.StatusText(rsp.status), time.Since(start))
}
return http.HandlerFunc(fn)
}
type responseLogger struct {
http.ResponseWriter
status int
}
func (r *responseLogger) WriteHeader(code int) {
r.status = code
r.ResponseWriter.WriteHeader(code)
}