tool.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package tool
  2. import (
  3. "encoding/json"
  4. "github.com/xinliangnote/go-gin-api/configs"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/repository/mysql"
  7. "github.com/xinliangnote/go-gin-api/internal/repository/redis"
  8. "github.com/xinliangnote/go-gin-api/pkg/file"
  9. "go.uber.org/zap"
  10. )
  11. type handler struct {
  12. logger *zap.Logger
  13. cache redis.Repo
  14. }
  15. func New(logger *zap.Logger, db mysql.Repo, cache redis.Repo) *handler {
  16. return &handler{
  17. logger: logger,
  18. cache: cache,
  19. }
  20. }
  21. func (h *handler) Cache() core.HandlerFunc {
  22. return func(ctx core.Context) {
  23. ctx.HTML("tool_cache", nil)
  24. }
  25. }
  26. func (h *handler) Data() core.HandlerFunc {
  27. return func(ctx core.Context) {
  28. ctx.HTML("tool_data", nil)
  29. }
  30. }
  31. func (h *handler) HashIds() core.HandlerFunc {
  32. return func(ctx core.Context) {
  33. ctx.HTML("tool_hashids", configs.Get())
  34. }
  35. }
  36. func (h *handler) Websocket() core.HandlerFunc {
  37. return func(ctx core.Context) {
  38. ctx.HTML("tool_websocket", nil)
  39. }
  40. }
  41. func (h *handler) Log() core.HandlerFunc {
  42. type logData struct {
  43. Level string `json:"level"`
  44. Time string `json:"time"`
  45. Path string `json:"path"`
  46. HTTPCode int `json:"http_code"`
  47. Method string `json:"method"`
  48. Msg string `json:"msg"`
  49. TraceID string `json:"trace_id"`
  50. Content string `json:"content"`
  51. CostSeconds float64 `json:"cost_seconds"`
  52. }
  53. type logsViewResponse struct {
  54. Logs []logData `json:"logs"`
  55. }
  56. type logParseData struct {
  57. Level string `json:"level"`
  58. Time string `json:"time"`
  59. Caller string `json:"caller"`
  60. Msg string `json:"msg"`
  61. Domain string `json:"domain"`
  62. Method string `json:"method"`
  63. Path string `json:"path"`
  64. HTTPCode int `json:"http_code"`
  65. BusinessCode int `json:"business_code"`
  66. Success bool `json:"success"`
  67. CostSeconds float64 `json:"cost_seconds"`
  68. TraceID string `json:"trace_id"`
  69. }
  70. return func(ctx core.Context) {
  71. readLineFromEnd, err := file.NewReadLineFromEnd(configs.ProjectAccessLogFile)
  72. if err != nil {
  73. h.logger.Error("NewReadLineFromEnd err", zap.Error(err))
  74. }
  75. logSize := 100
  76. obj := new(logsViewResponse)
  77. obj.Logs = make([]logData, logSize)
  78. for i := 0; i < logSize; i++ {
  79. content, _ := readLineFromEnd.ReadLine()
  80. if string(content) != "" {
  81. var logParse logParseData
  82. err = json.Unmarshal(content, &logParse)
  83. if err != nil {
  84. h.logger.Error("NewReadLineFromEnd json Unmarshal err", zap.Error(err))
  85. }
  86. data := logData{
  87. Content: string(content),
  88. Level: logParse.Level,
  89. Time: logParse.Time,
  90. Path: logParse.Path,
  91. Method: logParse.Method,
  92. Msg: logParse.Msg,
  93. HTTPCode: logParse.HTTPCode,
  94. TraceID: logParse.TraceID,
  95. CostSeconds: logParse.CostSeconds,
  96. }
  97. obj.Logs[i] = data
  98. }
  99. }
  100. ctx.HTML("tool_logs", obj)
  101. }
  102. }