package middleware import ( "fmt" "net/http" "perms-system-server/internal/response" "github.com/zeromicro/go-zero/core/limit" "github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/rest/httpx" ) type RateLimitMiddleware struct { limiter *limit.PeriodLimit } func NewRateLimitMiddleware(rds *redis.Redis, period int, quota int, keyPrefix string) *RateLimitMiddleware { limiter := limit.NewPeriodLimit(period, quota, rds, keyPrefix) return &RateLimitMiddleware{limiter: limiter} } func (m *RateLimitMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ip := r.Header.Get("X-Forwarded-For") if ip == "" { ip = r.Header.Get("X-Real-IP") } if ip == "" { ip = r.RemoteAddr } key := fmt.Sprintf("ip:%s", ip) code, _ := m.limiter.Take(key) if code == limit.OverQuota { httpx.ErrorCtx(r.Context(), w, response.ErrTooManyRequests("请求过于频繁,请稍后再试")) return } next(w, r) } }