This commit is contained in:
2025-11-03 12:24:01 +02:00
commit 0806865287
177 changed files with 18453 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
// internal/middleware/logger.go
package middleware
import (
"strconv"
"time"
"go-server/pkg/logger"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// Logger provides lightweight structured logging and response time header
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
latency := time.Since(start)
status := c.Writer.Status()
method := c.Request.Method
path := c.Request.URL.Path
ip := c.ClientIP()
ua := c.Request.UserAgent()
// Expose response time for clients/benchmarks
c.Header("X-Response-Time", strconv.FormatInt(latency.Microseconds(), 10)+"us")
// Structured log (zap is very fast and minimally blocking)
logger.Info("http_request",
zap.Int("status", status),
zap.String("method", method),
zap.String("path", path),
zap.Int64("latency_us", latency.Microseconds()),
zap.String("ip", ip),
zap.String("user_agent", ua),
)
}
}