Quick Start
This guide will help you set up GoPotency with the most common frameworks.
With Gin
Section titled “With Gin”The most popular way to use GoPotency is via its Gin middleware.
-
Initialize Storage: Create a storage backend (e.g., in-memory).
store := memory.NewMemoryStorage()// Note: Close the storage when donedefer store.Close() -
Create Manager: Configure and instantiate the idempotency manager.
manager, err := idempotency.NewManager(idempotency.Config{Storage: store,TTL: 24 * time.Hour,})defer manager.Close() -
Apply Middleware: Use the
Idempotencymiddleware in your Gin router.router := gin.Default()router.Use(ginmw.Idempotency(manager)) -
Define Routes: Your POST/PUT/PATCH routes are now protected.
router.POST("/orders", func(c *gin.Context) {c.JSON(200, gin.H{"status": "processed"})})
With Standard HTTP
Section titled “With Standard HTTP”GoPotency also works perfectly with the standard library net/http.
-
Initialize Manager: Same as above.
-
Wrap Handlers: Use the middleware to wrap your HTTP handlers.
mux := http.NewServeMux()handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.Write([]byte(`{"status": "processed"}`))})mux.Handle("/payment", httpmw.Idempotency(manager)(handler)) -
Start Server:
http.ListenAndServe(":8080", mux)