-
Notifications
You must be signed in to change notification settings - Fork 14
/
requestid_auth.go
91 lines (84 loc) · 2.3 KB
/
requestid_auth.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
package middleware
import (
"bytes"
"encoding/json"
"fmt"
"github.com/MQEnergy/gin-framework/global"
"github.com/gin-contrib/requestid"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"io/ioutil"
"strings"
"time"
)
type CustomResponseWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w CustomResponseWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w CustomResponseWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
// RequestIdAuth requestId中间件
func RequestIdAuth() gin.HandlerFunc {
return func(ctx *gin.Context) {
// 开始时间
reqStartTime := time.Now()
writer := CustomResponseWriter{
body: bytes.NewBufferString(""),
ResponseWriter: ctx.Writer,
}
ctx.Writer = writer
// 获取请求参数
reqBody := getRequestParams(ctx)
// 处理请求
ctx.Next()
fields := logrus.Fields{
"req_body": reqBody,
"req_host": ctx.Request.Host,
"req_method": ctx.Request.Method,
"req_clientIp": ctx.ClientIP(),
"req_id": requestid.Get(ctx),
"req_uri": ctx.Request.RequestURI,
"res_time": fmt.Sprintf("%s", time.Now().Sub(reqStartTime)), // 响应时间
}
if ctx.Writer.Status() != 200 {
responseData := writer.body.String()
fields["req_header"] = ctx.Request.Header
// 记录request日志
global.Logger.WithFields(fields).Warn(responseData)
} else {
global.Logger.WithFields(fields).Info("")
}
}
}
// getRequestParams 获取请求参数(GET,POST,PUT,DELETE)等
func getRequestParams(ctx *gin.Context) string {
if ctx.Request.Method == "GET" {
var params []string
values := ctx.Request.URL.Query()
for key, value := range values {
params = append(params, key+"="+value[0])
}
return strings.Join(params, "&")
}
if ctx.ContentType() != "application/json" {
ctx.Request.ParseForm()
return ctx.Request.PostForm.Encode()
}
rawData, _ := ctx.GetRawData()
//读取后,重新赋值 c.Request.Body ,以供后续的其他操作
ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData))
var m map[string]string
var params []string
// 反序列化
json.Unmarshal(rawData, &m)
for key, value := range m {
params = append(params, key+"="+value)
}
return strings.Join(params, "&")
}