package middleware import ( "bytes" "gin-vue-admin/global" "gin-vue-admin/model" "gin-vue-admin/service" "github.com/gin-gonic/gin" "io/ioutil" "net/http" "strconv" "time" ) func OperationRecord() gin.HandlerFunc { return func(c *gin.Context) { var body []byte if c.Request.Method != http.MethodGet { var err error body, err = ioutil.ReadAll(c.Request.Body) if err != nil { global.GVA_LOG.Error("read body from request error:", err) } else { c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) } } userId, err := strconv.Atoi(c.Request.Header.Get("x-user-id")) if err != nil { userId = 0 } record := model.SysOperationRecord{ Ip: c.ClientIP(), Method: c.Request.Method, Path: c.Request.URL.Path, Agent: c.Request.UserAgent(), Body: string(body), UserID: userId, } writer := responseBodyWriter{ ResponseWriter: c.Writer, body: &bytes.Buffer{}, } c.Writer = writer now := time.Now() c.Next() latency := time.Now().Sub(now) record.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String() record.Status = c.Writer.Status() record.Latency = latency record.Resp = writer.body.String() if err := service.CreateSysOperationRecord(record); err != nil { global.GVA_LOG.Error("create operation record error:", err) } } } type responseBodyWriter struct { gin.ResponseWriter body *bytes.Buffer } func (r responseBodyWriter) Write(b []byte) (int, error) { r.body.Write(b) return r.ResponseWriter.Write(b) }