operation.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package middleware
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "github.com/flipped-aurora/gin-vue-admin/server/global"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  10. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  11. "github.com/flipped-aurora/gin-vue-admin/server/service"
  12. "github.com/gin-gonic/gin"
  13. "go.uber.org/zap"
  14. )
  15. var operationRecordService = service.ServiceGroupApp.SystemServiceGroup.OperationRecordService
  16. func OperationRecord() gin.HandlerFunc {
  17. return func(c *gin.Context) {
  18. var body []byte
  19. var userId int
  20. if c.Request.Method != http.MethodGet {
  21. var err error
  22. body, err = ioutil.ReadAll(c.Request.Body)
  23. if err != nil {
  24. global.GVA_LOG.Error("read body from request error:", zap.Any("err", err))
  25. } else {
  26. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
  27. }
  28. }
  29. if claims, ok := c.Get("claims"); ok {
  30. waitUse := claims.(*request.CustomClaims)
  31. userId = int(waitUse.ID)
  32. } else {
  33. id, err := strconv.Atoi(c.Request.Header.Get("x-user-id"))
  34. if err != nil {
  35. userId = 0
  36. }
  37. userId = id
  38. }
  39. record := system.SysOperationRecord{
  40. Ip: c.ClientIP(),
  41. Method: c.Request.Method,
  42. Path: c.Request.URL.Path,
  43. Agent: c.Request.UserAgent(),
  44. Body: string(body),
  45. UserID: userId,
  46. }
  47. // 存在某些未知错误 TODO
  48. //values := c.Request.Header.Values("content-type")
  49. //if len(values) >0 && strings.Contains(values[0], "boundary") {
  50. // record.Body = "file"
  51. //}
  52. writer := responseBodyWriter{
  53. ResponseWriter: c.Writer,
  54. body: &bytes.Buffer{},
  55. }
  56. c.Writer = writer
  57. now := time.Now()
  58. c.Next()
  59. latency := time.Now().Sub(now)
  60. record.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String()
  61. record.Status = c.Writer.Status()
  62. record.Latency = latency
  63. record.Resp = writer.body.String()
  64. if err := operationRecordService.CreateSysOperationRecord(record); err != nil {
  65. global.GVA_LOG.Error("create operation record error:", zap.Any("err", err))
  66. }
  67. }
  68. }
  69. type responseBodyWriter struct {
  70. gin.ResponseWriter
  71. body *bytes.Buffer
  72. }
  73. func (r responseBodyWriter) Write(b []byte) (int, error) {
  74. r.body.Write(b)
  75. return r.ResponseWriter.Write(b)
  76. }