2022-10-11 22:44:11 +02:00
|
|
|
package limiter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Middleware registers the IP rate limiter middleware.
|
2024-01-14 18:28:17 +01:00
|
|
|
func Middleware(limiter *Limit) gin.HandlerFunc {
|
2022-10-11 22:44:11 +02:00
|
|
|
return func(c *gin.Context) {
|
2024-01-14 18:28:17 +01:00
|
|
|
if l := limiter.IP(c.ClientIP()); !l.Allow() {
|
2022-10-11 22:44:11 +02:00
|
|
|
c.AbortWithStatus(http.StatusTooManyRequests)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|