This is a generic middleware to rate limit HTTP requests.
package main
import (
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/storages"
"net/http"
"time"
)
func HelloHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, World!"))
}
func main() {
// 1. Create a request limiter storage.
storage := storages.NewInMemory()
// 2. Create a request limiter per handler.
http.Handle("/", tollbooth.LimitByIPFuncHandler(storage, tollbooth.NewRequestLimit(1, time.Second), HelloHandler))
http.ListenAndServe(":12345", nil)
}