OperationRecord 操作记录中间件
()
| 37 | |
| 38 | // OperationRecord 操作记录中间件 |
| 39 | func OperationRecord() gin.HandlerFunc { |
| 40 | return func(c *gin.Context) { |
| 41 | if strings.ToLower(c.Request.Method) == "get" { |
| 42 | c.Next() |
| 43 | return |
| 44 | } |
| 45 | operation := &Operation{} |
| 46 | operation.Result = 1 |
| 47 | operation.Time = time.Now() |
| 48 | operation.IP = strings.Split(c.Request.RemoteAddr, ":")[0] |
| 49 | rip := c.Request.Header.Get("X-Real-Ip") |
| 50 | if len(rip) > 0 { |
| 51 | operation.IP = rip |
| 52 | } |
| 53 | operation.Operator = c.GetString("username") |
| 54 | body, _ := ioutil.ReadAll(c.Request.Body) |
| 55 | c.Request.Body = ioutil.NopCloser(bytes.NewReader(body)) |
| 56 | operation.Method = c.Request.Method |
| 57 | operation.API = c.Request.URL.String() |
| 58 | operation.Body = string(body) |
| 59 | |
| 60 | c.Next() |
| 61 | |
| 62 | if strings.Contains(c.Request.URL.String(), "/operations") != true { |
| 63 | if c.Writer.Status() <= 599 && c.Writer.Status() >= 400 { |
| 64 | operation.Result = 0 |
| 65 | } |
| 66 | mydb.CreateOperation(operation) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func operationList(c *gin.Context) { |
| 72 | startTime := c.DefaultQuery("start_time", time.Now().Add(-time.Hour).Format("2006-01-02 15:04:05")) |
no test coverage detected