Skip to content

Echo middleware

Use the Echo adapter as standard middleware around application routes.

Prerequisites

  • Go 1.24 or newer
  • port 8080 available

Run

go run ./examples/echo
examples/echo/main.go
// Package main demonstrates using GoRL with the Echo middleware.
package main

import (
    "log"
    "net/http"
    "time"

    "github.com/AliRizaAynaci/gorl/v2"
    "github.com/AliRizaAynaci/gorl/v2/core"
    echomw "github.com/AliRizaAynaci/gorl/v2/middleware/echo"
    "github.com/labstack/echo/v4"
)

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

    e := echo.New()
    e.Use(echomw.RateLimit(limiter))

    e.GET("/", func(c echo.Context) error {
        return c.JSON(http.StatusOK, map[string]string{
            "message": "Hello from Echo!",
        })
    })

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

Expected behavior

Echo uses RealIP() as the default key. Five requests spend the initial token bucket capacity; the next request is denied until a token refills.

Production cautions

Configure Echo's proxy/IP extraction for your network and use authenticated identity where appropriate. Resource-aware middleware prefers the matched Echo route path and falls back to the raw request path.