-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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] question about Router.Match #683
Comments
I couldn't get your question correctly! Could you explain it with an example or some minimal code? If you're asking that for each route, the package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
fmt.Println(r.RequestURI + " logging middleware")
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func AuthenticationMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
fmt.Println(r.RequestURI + " authentication middleware")
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func AuthorizationMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
fmt.Println(r.RequestURI + " authorization middleware")
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world"))
}
func secondHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world"))
}
func thirdHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world"))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", handler)
r.Use(loggingMiddleware)
r.Use(AuthenticationMiddleware)
r.Use(AuthorizationMiddleware)
r.HandleFunc("/second-route", secondHandler)
r.HandleFunc("/third-route", thirdHandler)
http.ListenAndServe(":8000", r)
} Call the API: curl localhost:8000/second-route Log output: /second-routelogging middleware
/second-routeauthentication middleware
/second-routeauthorization middleware In simple terms. Middlewares are applicable to all the routes unless any middleware doesn't call I would recommend you to go through the comment #445 (comment) |
@dongzeXD can this be closed, or is there any additional info you need? |
location package mux. mux.go
Does this mean that each Match has to construct a invoke chain?
The text was updated successfully, but these errors were encountered: