-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_middleware.go
65 lines (57 loc) · 1.45 KB
/
gin_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
package audit_log
import (
"log"
"github.com/gin-gonic/gin"
)
func NewAuditLogGinMiddleware(saveFn func(op *OperateLogs) error) gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
go func() {
op, exists := c.Get(KEY_LOG)
if !exists {
return
}
operateLog, ok := op.(*OperateLogs)
if !ok {
log.Print("op.(*OperateLogs) err")
return
}
operateLog.IP = convIP(c.Request.Header)
operateLog.AccountID = c.GetUint("id")
operateLog.GroupID = c.GetUint("groupId")
needFields := []string{}
if val, exists := c.Get(KEY_NEED_FIELDS); exists {
needFields, ok = val.([]string)
if !ok {
log.Print("needFields get err")
return
}
}
expectFields := []string{}
if val, exists := c.Get(KEY_EXPECT_FIELDS); exists {
expectFields, ok = val.([]string)
if !ok {
log.Print("existsFields get err")
return
}
}
beforeFields, existsBefore := c.Get(KEY_BEFORE)
afterFields, existsAfter := c.Get(KEY_AFTER)
if existsBefore || existsAfter {
fieldsSlice, beforeSlice, afterSlice, err := GetFieldsLogSlice(beforeFields, afterFields, needFields, expectFields)
if err != nil {
log.Printf("GetFieldsLogSlice err %v", err)
return
}
operateLog.Fields = fieldsSlice
operateLog.FieldsBefore = beforeSlice
operateLog.FieldsAfter = afterSlice
}
err := saveFn(operateLog)
if err != nil {
log.Printf("create operateLog %v", err)
return
}
}()
}
}