package middleware import ( "time" "github.com/gin-gonic/gin" "dating-app/internal/models" "dating-app/internal/monitoring" ) // Metrics هر درخواست رو (متد، مسیر، کد پاسخ، مدت زمان) توی Store ثبت می‌کنه. // باید قبل از Recovery ثبت بشه (بیرونی‌تر) تا حتی درخواست‌هایی که پنیک می‌کنن // و توسط Recovery مهار می‌شن هم توی آمار لحاظ بشن. func Metrics(store *monitoring.Store) gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() defer func() { path := c.FullPath() if path == "" { path = c.Request.URL.Path } rec := monitoring.RequestRecord{ Time: start, Method: c.Request.Method, Path: path, StatusCode: c.Writer.Status(), DurationMs: float64(time.Since(start).Microseconds()) / 1000.0, ClientIP: c.ClientIP(), } if userVal, exists := c.Get(ContextUserKey); exists { if user, ok := userVal.(*models.User); ok { id := user.ID rec.UserID = &id } } store.Record(rec) }() c.Next() } }