第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

Go
BIG陽(yáng) 2022-07-11 16:47:40
我對(duì) Gorilla mux 路由有一個(gè)特定要求,我想為一個(gè)子路由器下的不同路由添加不同的中間件(在我的例子中是 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 路由器。我還有兩個(gè)名為 ValidateAccessToken 和 ValidateRefreshToken 的中間件函數(shù)。我想將 ValidateRefreshToken 中間件函數(shù)用于“/refresh-token”路由,并將 ValidateAccessToken 用于 GET 子路由器下的所有其他路由。我想用 Gorilla mux 路由本身來做到這一點(diǎn)。請(qǐng)建議我完成上述場(chǎng)景的適當(dāng)方法。感謝您的時(shí)間和精力。
查看完整描述

2 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

notorious.no 提供的解決方案也非常適合給定的要求。我還遇到了另一個(gè)使用 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)

使用參考來到這個(gè)解決方案:https ://github.com/gorilla/mux/issues/360


查看完整回答
反對(duì) 回復(fù) 2022-07-11
?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

我有一個(gè)類似的用例,這是我如何解決它的一個(gè)例子:


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函數(shù)允許您將多個(gè)處理程序鏈接在一起。我已經(jīng)演示了 2 個(gè)中間件函數(shù)(ValidateRefreshToken和MethodLogger)。中間件基本上是閉包。您可以將此方法與任何接受http.Handler諸如mux和的框架一起使用chi。


查看完整回答
反對(duì) 回復(fù) 2022-07-11
  • 2 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)