Echo Middleware
GoPotency provides a seamless integration with the Echo framework through its Echo-specific middleware.
Installation
Section titled “Installation”Ensure you have GoPotency installed:
go get github.com/fco-gt/gopotencyBasic Usage
Section titled “Basic Usage”package main
import ( "net/http"
idempotency "github.com/fco-gt/gopotency" echomw "github.com/fco-gt/gopotency/middleware/echo" "github.com/fco-gt/gopotency/storage/memory" "github.com/labstack/echo/v4")
func main() { e := echo.New()
// 1. Initialize storage and manager store := memory.NewMemoryStorage() manager, _ := idempotency.NewManager(idempotency.Config{ Storage: store, })
// 2. Register middleware e.Use(echomw.Idempotency(manager))
// 3. Define routes e.POST("/orders", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{"status": "captured"}) })
e.Logger.Fatal(e.Start(":8080"))}Configuration
Section titled “Configuration”You can customize the middleware behavior by passing a Config object to the NewManager function. See the Configuration Guide for more details.
Requiring Keys
Section titled “Requiring Keys”For critical routes, you can enforce that a key must be present:
manager, _ := idempotency.NewManager(idempotency.Config{ Storage: store, RequireKey: true, // Will return 400 if header is missing})Custom Key Strategy
Section titled “Custom Key Strategy”By default, the middleware looks for the Idempotency-Key header. You can change this or use a custom strategy (like request body hashing) in the manager configuration.