I am using gorilla mux for request routing.
I wrote a basic middleware which I want to add user
variable to context for reach in handlers. But I chould not found how can I get route parameter in the middleware:
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/{username}/accounts", AccountListHandler)
log.Fatal(http.ListenAndServe(":8080", AuthMiddleware(router)))
Middleware code:
func AuthMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// params := mux.Vars(r)
// fmt.Printf("%v", params["username"])
user := User{Username: "myUsername"}
ctx := context.WithValue(r.Context(), "user", user)
h.ServeHTTP(w, r.WithContext(ctx))
})
}
I want to reach username
paramter in the middleware. How can I do this?