-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] Application failed against latest mux cod base. Any change required? #528
Comments
This pull request related to schemes was recently merged: #474 You are calling router.Schemes("http").Methods("GET").Path("/api").HandlerFunc(restHandler) To further demonstrate why this is happening, take this code for example: package main
import (
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.Schemes("http").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hi from scheme handler"))
})
router.Methods("GET").Path("/api").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("hi from other handler"))
})
http.ListenAndServe(":8080", router)
} Since we register the scheme handler first, it is going to match all of our http requests and intercept all of them before we even get to the path handler. If you reverse the order that we register them, you will notice that we match on the path handler and then fall back to the scheme handler on any other http requests. |
Hi, thanks for the reply. router.Methods("GET").Path("/api").HandlerFunc(restHandler) if true, does it mean that it will match https request as well? |
If you want to register multiple paths that only match on HTTP, you would first create a scheme matcher route for HTTP, and then register method/path matchers on that route. You could use a sub router, and it might look something like this: package main
import (
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
httpOnly := router.Schemes("http").Subrouter()
httpOnly.Methods("GET").Path("/api").HandlerFunc(func(res http.ResponseWriter, _ *http.Request) { res.Write([]byte("root")) })
httpOnly.Methods("GET").Path("/api/a").HandlerFunc(func(res http.ResponseWriter, _ *http.Request) { res.Write([]byte("a")) })
httpOnly.Methods("GET").Path("/api/b").HandlerFunc(func(res http.ResponseWriter, _ *http.Request) { res.Write([]byte("b")) })
http.ListenAndServe(":8080", router)
} It will match https if you add https to the list of schemes. Keep in mind that if you don't add a Schemes check at all it will match http and https anyways. |
Thanks for the advice. This works out well. |
Describe the problem you're having
it's noticed that URL is either not registered properly or not identifiable when complied with
latest mux code base.
884b5ff.
Are there new changes required to get it to work with the latest?
…
Versions
"Show me the code!"
Register url path:
func registerPaths() *mux.Router {
router := mux.NewRouter()
router.Schemes("http")
router.Methods("GET").Path("/api").HandlerFunc(restHandler)
return router
}
Server initialization:
func Init(router *mux.Router) {
}
…
…
The text was updated successfully, but these errors were encountered: