diff --git a/app.go b/app.go index cc4f32ffc319ffa8fb521dcf153e188f7911524e..bfdbcc13229f87506ed6daefacdeaa6fe1bc7232 100644 --- a/app.go +++ b/app.go @@ -6,12 +6,10 @@ import ( "net" "os" "sync/atomic" - "time" "github.com/gofiber/contrib/hcaptcha" "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" - "github.com/gofiber/fiber/v3/middleware/limiter" "github.com/syndtr/goleveldb/leveldb" ) @@ -24,18 +22,6 @@ func serve(sig chan os.Signal, db *leveldb.DB) error { AllowHeaders: []string{"Origin", "Content-Type", "Accept"}, })) - // rate limiting - app.Use(limiter.New(limiter.Config{ - Max: 5, - Expiration: 7 * 24 * time.Hour, // 1 week expiration - LimitReached: func(c fiber.Ctx) error { - log.Printf("Rate limit exceeded for IP: %s", c.IP()) - return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{ - "message": "Rate limit exceeded. Max 5 registrations per week.", - }) - }, - })) - var captcha fiber.Handler hCaptchaEnable := conf[hCaptchaSiteKey] != "unset" && conf[hCaptchaSecretKey] != "unset" diff --git a/limiter.go b/limiter.go new file mode 100644 index 0000000000000000000000000000000000000000..8ebc02ec089a6a10f4f0dbbf78479044afe1585e --- /dev/null +++ b/limiter.go @@ -0,0 +1,22 @@ +package main + +import ( + "log" + "time" + + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/limiter" +) + +func rateLimiter() fiber.Handler { + return limiter.New(limiter.Config{ + Max: 5, + Expiration: 7 * 24 * time.Hour, // 1 week expiration + LimitReached: func(c fiber.Ctx) error { + log.Printf("Rate limit exceeded for IP: %s", c.IP()) + return c.Status(fiber.StatusTooManyRequests).JSON(fiber.Map{ + "message": "Rate limit exceeded. Max 5 registrations per week.", + }) + }, + }) +} diff --git a/register.go b/register.go index 3ee69853f302a4b9acac4ee625cf3826cb997ce0..00c5cdd41db0442fe2772bbad1f8aa71009fd79c 100644 --- a/register.go +++ b/register.go @@ -19,7 +19,7 @@ type registration struct { // Waitlist registration route func routeRegister(app *fiber.App, p string, db *leveldb.DB, count *atomic.Uint64, captcha fiber.Handler) { - app.Post(p, func(c fiber.Ctx) error { + app.Post(p, rateLimiter(), func(c fiber.Ctx) error { t := time.Now().UTC() req := new(registration)