-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
40 lines (33 loc) · 1.03 KB
/
debug.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
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"strings"
"github.com/codegangsta/negroni"
"github.com/spf13/viper"
)
type debugMiddleware struct {
}
func (h *debugMiddleware) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
if viper.GetBool("debug") || req.URL.Query().Get("debug") == "true" {
// Save a copy of this request for debugging.
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
}
// 如果请求 body 是 json 格式,可以使用下面的方法打印
if req.Header.Get("Content-Type") == "application/json" {
fmt.Println(string(requestDump))
} else {
// 否则只输出请求头
fmt.Println(string(requestDump[:strings.Index(string(requestDump), "\r\n\r\n")]))
}
}
next(w, req)
}
// NewMiddleware 创建新的UID中间件
func NewMiddleware(cfg map[string]interface{}) (negroni.Handler, error) {
return &debugMiddleware{}, nil
}
// https://medium.com/doing-things-right/pretty-printing-http-requests-in-golang-a918d5aaa000