Skip to content

Fiber middleware

Use the Fiber adapter to apply a limiter before application handlers.

Prerequisites

  • Go 1.24 or newer
  • port 3000 available

Run

go run ./examples/fiber
examples/fiber/main.go
// Package main demonstrates using GoRL with the Fiber middleware.
package main

import (
    "log"
    "time"

    "github.com/AliRizaAynaci/gorl/v2"
    "github.com/AliRizaAynaci/gorl/v2/core"
    fibermw "github.com/AliRizaAynaci/gorl/v2/middleware/fiber"
    "github.com/gofiber/fiber/v2"
)

func main() {
    limiter, err := gorl.New(core.Config{
        Strategy: core.FixedWindow,
        Limit:    5,
        Window:   30 * time.Second,
    })
    if err != nil {
        log.Fatal(err)
    }
    defer limiter.Close()

    app := fiber.New()
    app.Use(fibermw.RateLimit(limiter))

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello from Fiber!")
    })

    log.Fatal(app.Listen(":3000"))
}
for i in 1 2 3 4 5 6; do curl -i http://localhost:3000/; done

Expected behavior

Fiber uses c.IP() as the default key. Five requests are allowed in a thirty- second fixed window; the next receives a JSON 429 response.

Production cautions

Verify Fiber proxy settings and forwarded-IP behavior in the actual deployment. The resource-scoped default uses c.Path(); normalize high-cardinality paths or supply a stable ResourceFunc when URLs contain identifiers.