Skip to content
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

Open
dongzeXD opened this issue Jul 1, 2022 · 2 comments
Open

[question] question about Router.Match #683

dongzeXD opened this issue Jul 1, 2022 · 2 comments
Labels

Comments

@dongzeXD
Copy link

dongzeXD commented Jul 1, 2022

location package mux. mux.go
Does this mean that each Match has to construct a invoke chain?
image

@amustaque97
Copy link
Contributor

amustaque97 commented Jul 2, 2022

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 Match method will construct middleware even though it is not defined for routes like: second-route and third-route? Then yes, r.middleware is defined as a global so whenever there is any new middleware then it will update the middleware slice here and will be applied to all the routes.

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 next.ServeHTTP()

I would recommend you to go through the comment #445 (comment)

@ianling
Copy link

ianling commented Aug 9, 2022

@dongzeXD can this be closed, or is there any additional info you need?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants