Skip to content
Snippets Groups Projects
Commit 52c3efc7 authored by Levatax's avatar Levatax
Browse files

fix(api): apply limiter to register route and move logic to limiter.go

parent 217dee64
Branches
Tags
1 merge request!9fix(api): apply limiter to register route and move logic to limiter.go
Pipeline #978 passed
......@@ -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"
......
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.",
})
},
})
}
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment