FreePad/lib/controllers/controllers_ratelimiter.go

35 lines
736 B
Go
Raw Permalink Normal View History

2022-05-22 19:38:12 +03:00
package controllers
import (
"time"
"github.com/JustKato/FreePad/lib/helper"
"github.com/gin-gonic/gin"
"github.com/ulule/limiter/v3"
"github.com/ulule/limiter/v3/drivers/store/memory"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
)
// Apply the rate limiter to the gin Engine
func DoRateLimit(router *gin.Engine) {
// Initialize the rate limiter
rate := limiter.Rate{
Period: 5 * time.Minute,
Limit: int64(helper.GetApiBanLimit()),
}
// Initialize the memory storage
store := memory.NewStore()
// Initialize the limiter instance
instance := limiter.New(store, rate)
// Create the gin middleware
middleWare := mgin.NewMiddleware(instance)
// use the middleware in gin
router.Use(middleWare)
}