- Restructure handlers into file_commands/file_queries/file_handlers - Add gRPC auth client, JWT middleware, rate limiting, request ID - Add common utilities: logger, sanitizer, s3_errors - Add unit tests for config, mediator, auth, request_id, sanitize - Add proto definitions and generated code - Remove old web UI pages - Add .dockerignore and .env.example
25 lines
480 B
Go
25 lines
480 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
func RateLimitMiddleware(rps float64, burst int) gin.HandlerFunc {
|
|
var limiters sync.Map
|
|
|
|
return func(c *gin.Context) {
|
|
key := c.ClientIP()
|
|
l, _ := limiters.LoadOrStore(key, rate.NewLimiter(rate.Limit(rps), burst))
|
|
if !l.(*rate.Limiter).Allow() {
|
|
c.JSON(http.StatusTooManyRequests, gin.H{"error": "too many requests"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|