update: 整理项目结构 重写中间件 提高复用性

This commit is contained in:
2025-09-26 12:29:57 +08:00
parent 5daf6df318
commit d15352a18b
9 changed files with 430 additions and 324 deletions

View File

@@ -0,0 +1,31 @@
package middleware
import (
"log/slog"
"time"
"github.com/gin-gonic/gin"
)
// Logger 使用 slog 输出结构化访问日志
func Logger(logger *slog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
c.Next()
if raw != "" { path = path + "?" + raw }
latency := time.Since(start)
status := c.Writer.Status()
logger.Info("HTTP请求",
"req_id", GetReqID(c),
"method", c.Request.Method,
"path", path,
"status", status,
"latency_ms", latency.Milliseconds(),
"size", c.Writer.Size(),
"ip", c.ClientIP(),
"ua", c.GetHeader("User-Agent"),
)
}
}