-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
188 lines (162 loc) · 4.11 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
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
package httpkit
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
)
var (
// RequestLogger 请求日志记录,可替换
RequestLogger = func(w http.ResponseWriter, r *http.Request, logger logrus.FieldLogger) logrus.FieldLogger {
rl := r.URL
fields := logrus.Fields{
"remote": r.RemoteAddr,
"method": r.Method,
"path": rl.Path,
}
if v := rl.RawQuery; v != "" {
if q := rl.Query(); len(q) > 0 {
fields["query"] = q
}
}
if m := r.Method; m == http.MethodPost || m == http.MethodPut || m == http.MethodPatch {
r.ParseForm()
if form := r.PostForm; len(form) > 0 {
fields["form"] = form
}
}
return logger.WithFields(fields)
}
responsePool = sync.Pool{
New: func() interface{} {
return &responseWrapper{}
},
}
requestBodyPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
)
type responseWrapper struct {
http.ResponseWriter
status int
}
func (rw *responseWrapper) WriteHeader(statusCode int) {
rw.status = statusCode
rw.ResponseWriter.WriteHeader(statusCode)
}
func (rw responseWrapper) StatusCode() int {
if c := rw.status; c > 0 {
return c
}
return http.StatusOK
}
// LogRequest http访问日志
func LogRequest(logger logrus.FieldLogger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := responsePool.Get().(*responseWrapper)
ww.ResponseWriter = w
ww.status = 0
defer func() {
ww.ResponseWriter = nil
responsePool.Put(ww)
}()
start := time.Now()
var rBody *bytes.Buffer
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
if n, _ := strconv.Atoi(r.Header.Get("Content-Length")); n > 0 && n <= 8192 {
rBody = requestBodyPool.Get().(*bytes.Buffer)
rBody.Reset()
defer func() {
requestBodyPool.Put(rBody)
}()
r.Body = io.NopCloser(io.TeeReader(r.Body, rBody))
}
}
next.ServeHTTP(ww, r)
status := ww.StatusCode()
fl := RequestLogger(w, r, logger).
WithFields(logrus.Fields{
"duration": time.Since(start).Milliseconds(),
"status": status,
})
if rBody != nil && rBody.Len() > 0 {
fl = fl.WithField("body", rBody.String())
}
if status >= 500 {
fl.Error("http request")
} else if status >= 400 {
fl.Warn("http request")
} else {
fl.Info("http request")
}
})
}
}
// Recoverer recover panic
func Recoverer(logger logrus.FieldLogger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if v := recover(); v != nil {
switch vv := v.(type) {
case *Error:
if err := errors.Unwrap(vv); err != nil {
entry := logger.WithError(err)
if f, ok := vv.Caller(); ok {
entry = entry.WithFields(logrus.Fields{
logrus.FieldKeyFile: fmt.Sprintf("%s:%d", f.File, f.Line),
logrus.FieldKeyFunc: f.Function,
})
} else {
entry = entry.WithFields(logrus.Fields{
"method": r.Method,
"uri": r.URL.Path,
})
}
if code := vv.StatusCode(); code >= http.StatusInternalServerError {
entry.Error("recover http error")
} else {
entry.Debug("recover http error")
}
}
if err := WriteError(w, vv); err != nil {
logger.WithError(err).Error("send error response")
}
return
case error:
fields := logrus.Fields{
"method": r.Method,
"uri": r.URL.Path,
}
if _, ok := vv.(runtime.Error); ok {
fields["stack"] = string(debug.Stack())
}
logger.WithError(vv).
WithFields(fields).
Error("recover panic")
default:
logger.WithField("error", v).
WithFields(logrus.Fields{
"method": r.Method,
"uri": r.URL.Path,
}).
Error("recover panic")
}
w.WriteHeader(http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}
}