youtu_ecpm/main.go
2025-01-13 18:08:17 +08:00

38 lines
867 B
Go

package main
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"strconv"
"time"
viperConfig "youtu_ecpm/config"
"youtu_ecpm/log"
"youtu_ecpm/router"
)
func main() {
r := gin.New()
r.Use(gin.Recovery(), zapMiddleware(log.ZapLog))
router.InitGin(r)
r.Run(":" + strconv.Itoa(viperConfig.GetPort())) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
// zapMiddleware 记录每个请求的基本信息
func zapMiddleware(zapLogger *log.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
// 请求开始时间
start := time.Now()
// 处理请求
c.Next()
// 记录请求信息
zapLogger.Info("HTTP Request",
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.String("status", strconv.Itoa(c.Writer.Status())),
zap.String("duration", time.Since(start).String()),
)
}
}