httprouter middleware for rate limiting HTTP requests.
package main
import (
"time"
"log"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth_httprouter"
"github.com/julienschmidt/httprouter"
)
Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func main() {
router := httprouter.New()
// Create a limiter struct.
limiter := tollbooth.NewLimiter(1, time.Second, nil)
// Index route without limiting.
router.GET("/", Index)
// Hello route with limiting.
router.GET("/hello/:name",
tollbooth_httprouter.LimitHandler(Hello, limiter),
)
log.Fatal(http.ListenAndServe(":8080", router))
}