I want to achieve routes like
user/profile
user/cart
user/products
Currently, I'm doing this
r.HandleFunc("user/signup", signupHandler).Methods("POST")
r.HandleFunc("user/signin", signinHandler).Methods("POST")
r.HandleFunc("user/profile", profileHandler).Methods("GET")
r.HandleFunc("user/cart", cartHandler).Methods("POST")
r.HandleFunc("user/products", productsHandler).Methods("GET")
As you can see these routes starts with user
so can how can I know it's a user
routes so I can send handle it in a different file.
I want something like
r.HandleFunc("user/", handlerWhichHandelAllTheRequestFromUser)
It should handle all the URL which starts from users
.
I'm using mux gorilla
There is a PathPrefix function in mux. You can use it to make the path prefix i.e. "/user" in this case.
example:-
userHandler.go
func makeUserHandler()http.Handler{
//Define all you handlers here
r := mux.NewRouter()
r.HandleFunc("user/signup", signupHandler).Methods("POST")
r.HandleFunc("user/signin", signinHandler).Methods("POST")
r.HandleFunc("user/profile", profileHandler).Methods("GET")
r.HandleFunc("user/cart", cartHandler).Methods("POST")
r.HandleFunc("user/products", productsHandler).Methods("GET")
return r
}
main.go
r := mux.NewRouter()
r.PathPrefix("/user").Handler(makeUserHandler())
Please make necessary changes to the above code as per your requirement.