Reputation: 1295
I have a routes.go file that looks like this:
func GetRouter(services Servicesr) *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/api/)
return router
}
I want to rate limit my /api/services, and I am trying to do something like this:
in limiter.go I have this:
package limiter
import (
"golang.org/x/time/rate"
"net/http"
)
var limiter = rate.NewLimiter(1, 3)
func Limit(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if limiter.Allow() == false {
http.Error(w, http.StatusText(429), http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
})
}
but I can't seem to grasp my head around how I can rate limit the router.HandlerFunc for api/services
Upvotes: 0
Views: 858
Reputation:
Change the router.HandleFunc
line for api/services
to the following:
router.Handle("/api/services", Limit(http.HandlerFunc(services.GetServices))).Methods(http.MethodGet)
Upvotes: 2