如何在大猩猩多路复用器的获取子路由器中为特定路由使用特定中间件

How to Use Specific middleware for specific routes in a Get Subrouter in gorilla mux

我对 Gorilla mux 路由有一个特定的要求,我想为一个子路由器下的不同路由添加不同的中间件(在我的例子中是 GET 子路由器)。下面是我的路由代码:

    // create a serve mux
    sm := mux.NewRouter()

    // register handlers
    postR := sm.Methods(http.MethodPost).Subrouter()
    postR.HandleFunc("/signup", uh.Signup)
    postR.HandleFunc("/login", uh.Login)
    postR.Use(uh.MiddlewareValidateUser)

    getR := sm.Methods(http.MethodGet).Subrouter()
    getR.HandleFunc("/refresh-token", uh.RefreshToken)
    getR.HandleFunc("/user-profile", uh.GetUserProfile)

在上面的路由器逻辑中,我的 /refresh-token 和 /user-profile 令牌都在 getR 路由器下。我还有两个中间件函数,称为 ValidateAccessToken 和 ValidateRefreshToken。我想为“/refresh-token”路由使用 ValidateRefreshToken 中间件函数,为 GET 子路由器下的所有其他路由使用 ValidateAccessToken。我想用 Gorilla mux 路由本身来完成。请建议我采用适当的方法来完成上述场景。感谢您的时间和努力。

我有一个类似的用例,这是我如何解决它的一个例子:

package main

import (
    "log"
    "net/http"
    "time"
)

import (
    "github.com/gorilla/mux"
)

// Adapter is an alias so I dont have to type so much.
type Adapter func(http.Handler) http.Handler

// Adapt takes Handler funcs and chains them to the main handler.
func Adapt(handler http.Handler, adapters ...Adapter) http.Handler {
    // The loop is reversed so the adapters/middleware gets executed in the same
    // order as provided in the array.
    for i := len(adapters); i > 0; i-- {
        handler = adapters[i-1](handler)
    }
    return handler
}

// RefreshToken is the main handler.
func RefreshToken(res http.ResponseWriter, req *http.Request) {
    res.Write([]byte("hello world"))
}

// ValidateRefreshToken is the middleware.
func ValidateRefreshToken(hKey string) Adapter {
    return func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
            // Check if a header key exists and has a value
            if value := req.Header.Get(hKey); value == "" {
                res.WriteHeader(http.StatusForbidden)
                res.Write([]byte("invalid request token"))
                return
            }

            // Serve the next handler
            next.ServeHTTP(res, req)
        })
    }
}

// MethodLogger logs the method of the request.
func MethodLogger() Adapter {
    return func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
            log.Printf("method=%s uri=%s\n", req.Method, req.RequestURI)
            next.ServeHTTP(res, req)
        })
    }
}

func main() {
    sm := mux.NewRouter()
    getR := sm.Methods(http.MethodGet).Subrouter()
    getR.HandleFunc("/refresh-token", Adapt(
        http.HandlerFunc(RefreshToken),
        MethodLogger(),
        ValidateRefreshToken("Vikee-Request-Token"),
    ).ServeHTTP)

    srv := &http.Server{
        Handler:      sm,
        Addr:         "localhost:8888",
        WriteTimeout: 30 * time.Second,
        ReadTimeout:  30 * time.Second,
    }
    log.Fatalln(srv.ListenAndServe())
}

Adapt 函数可让您将多个处理程序链接在一起。我已经演示了 2 个中间件函数(ValidateRefreshTokenMethodLogger)。中间件基本上是闭包。您可以将此方法用于任何接受 http.Handler 的框架,例如 muxchi.

notorious.no 提供的解决方案也非常适合给定的要求。我还遇到了另一个使用 PathPrefix 并解决了相同问题的解决方案,并希望获得相同的建议。

    // create a serve mux
    sm := mux.NewRouter()

    // register handlers
    postR := sm.Methods(http.MethodPost).Subrouter()
    postR.HandleFunc("/signup", uh.Signup)
    postR.HandleFunc("/login", uh.Login)
    postR.Use(uh.MiddlewareValidateUser)

    refToken := sm.PathPrefix("/refresh-token").Subrouter()
    refToken.HandleFunc("", uh.RefreshToken)
    refToken.Use(uh.MiddlewareValidateRefreshToken)

    getR := sm.Methods(http.MethodGet).Subrouter()
    getR.HandleFunc("/greet", uh.Greet)
    getR.Use(uh.MiddlewareValidateAccessToken)

使用参考资料得出此解决方案:https://github.com/gorilla/mux/issues/360

还有一个

使用示例

mux := mux.NewRouter()
healthRoute := mux.Path("/health").Handler(healthHandler)
mux.PathPrefix("/").Handler(defaultHandler)

// Run logging middleware except on the health route because health is spammy
mux.Use(libHttp.MiddlewareExcept(libHttp.LoggingMiddlewareWith404(logger), healthRoute))

并实施

// MiddlewareExcept returns a new middleware that calls the provided middleware except on the provided routes
func MiddlewareExcept(middleware mux.MiddlewareFunc, routes ...*mux.Route) mux.MiddlewareFunc {
    routeMatch := mux.RouteMatch{}
    return func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
            for _, route := range routes {
                if route.Match(r, &routeMatch) {
                    if next != nil {
                        next.ServeHTTP(rw, r)
                    }
                } else {
                    middleware(next).ServeHTTP(rw, r)
                }
            }
        })
    }
}