1
0
mirror of https://github.com/JustKato/FreePad.git synced 2026-02-23 15:50:46 +02:00

+ Working on the API Rate limiting

This commit is contained in:
2022-05-22 19:38:12 +03:00
parent 53b35745cd
commit 916bcae961
5 changed files with 153 additions and 4 deletions

View File

@@ -0,0 +1,34 @@
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)
}

View File

@@ -16,6 +16,25 @@ func GetDomainBase() string {
return domainBase
}
func GetApiBanLimit() int {
banLimit, exists := os.LookupEnv("API_BAN_LIMIT")
if !exists {
os.Setenv("API_BAN_LIMIT", "300")
banLimit = "300"
}
// Try and convert the string into an integer
rez, err := strconv.Atoi(banLimit)
// Check if the conversion has failed
if err != nil {
// Simply return the default
return 300
}
return rez
}
func GetMaximumPadSize() int {
// Lookup if the maximum pad size variable exists.
maxPadSize, exists := os.LookupEnv("MAXIMUM_PAD_SIZE")