如何在 gorilla mux 的 Get Subrouter 中为特定路由使用特定中间件

vig*_*esh 2 routes go gorilla

我对 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)
Run Code Online (Sandbox Code Playgroud)

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

not*_*.no 7

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

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())
}
Run Code Online (Sandbox Code Playgroud)

Adapt函数允许您将多个处理程序链接在一起。我已经演示了 2 个中间件函数(ValidateRefreshTokenMethodLogger)。中间件基本上都是闭包。您可以将此方法与任何接受http.Handler诸如mux和 之类的框架一起使用chi


vig*_*esh 5

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)
Run Code Online (Sandbox Code Playgroud)

使用参考来到这个解决方案:https : //github.com/gorilla/mux/issues/360