Negroni middleware for rate limiting HTTP requests.
package main
import (
"github.com/urfave/negroni"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth_negroni"
"net/http"
"time"
)
func HelloHandler() http.Handler {
handleFunc := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world!"))
}
return http.HandlerFunc(handleFunc)
}
func main() {
// Create a limiter struct.
limiter := tollbooth.NewLimiter(1, time.Second, nil)
mux := http.NewServeMux()
mux.Handle("/", negroni.New(
tollbooth_negroni.LimitHandler(limiter),
negroni.Wrap(HelloHandler()),
))
n := negroni.Classic()
n.UseHandler(mux)
n.Run(":12345")
}