// Package middleware 提供 Guardrail 与限流等接入层中间件。 package middleware import ( "net/http" "time" "github.com/gin-gonic/gin" "github.com/sundynix/sundynix-gateway/internal/store" ) // Guardrail 实现 Harness 的输入/输出护栏(敏感词、注入、配额校验等)。 func Guardrail() gin.HandlerFunc { return func(c *gin.Context) { // TODO: 输入护栏校验 c.Next() // TODO: 输出护栏校验 } } // RateLimit 基于 Redis 的会话级限流(按客户端 IP,每分钟上限)。 // Redis 降级时 Allow 始终放行,不阻断业务。 func RateLimit(cache *store.Redis) gin.HandlerFunc { const perMinute = 120 return func(c *gin.Context) { ok, _ := cache.Allow(c.Request.Context(), c.ClientIP(), perMinute, time.Minute) if !ok { c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{"error": "rate limit exceeded"}) return } c.Next() } }