I am running a server with below code:
// Assuming there is no import error
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "File not found", http.StatusNotFound)
})
n := negroni.Classic()
n.Use(negroni.HandlerFunc(bodmas.sum(4,5)))
n.UseHandler(mux)
n.Run(":4000" )
It works perfectly fine.
But when I wrap bodmas.sum with another http handler I always get "File not found." The flow does not go to this route.
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "File not found", http.StatusNotFound)
})
n := negroni.Classic()
n.Use(negroni.HandlerFunc(wrapper.RateLimit(bodmas.sum(4,5),10)))
n.UseHandler(mux)
n.Run(":" + cfg.Server.Port)
}
wrapper.RateLimit is define as below. This works as expected when tested separately:
func RateLimit(h resized.HandlerFunc, rate int) (resized.HandlerFunc) {
:
:
// logic here
rl, _ := ratelimit.NewRateLimiter(rate)
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
if rl.Limit(){
http.Error(w, "Gateway Timeout", http.StatusGatewayTimeout )
} else {
next(w, r)
}
}
}
There is no error. Any suggestions about this behavior ? How to make it work ?
I am not sure what sure what is the problem with this code, but it appears to be not the
negorniway.negronimight not behave as expected if we wrap its handler with anotherhttp.Handlerfunc. So, I modified my code and got the work done bymiddleware.My current code looks something like as below:
wrapper.gohas :Hope it helps someone.