dongmei6426
2017-05-14 13:31
浏览 121
已采纳

在goLang中使用julienschmidt / httprouter时如何使用中间件?

What's the best way to chain middlewares while using julienschmidt/httprouter?

As far as I have googled, http.HandlerFunc accepts functions only in the form func (w ResponseWriter, r *Request) whereas httprouter.Handle functions are of the form func (w http.ResponseWriter, r *http.Request, ps httprouter.Params).

How to I chain middlewares without converting the httprouter.Handle function into http.HandlerFunc?

For example:
My routes.go is of the form,

router  :=  httprouter.New()
router.POST("/api/user/create", middlewares.EscapeStringsMiddleware(User.CreateUser))
log.Fatal(http.ListenAndServe(":8000",  router))

How do I write middleware functions for the above mentioned route?

Already tried methods:

1.

func EscapeStringsMiddleware(next http.Handler) httprouter.Handle {

    return func (response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
        err := request.ParseForm()
        if err != nil {
            panic(err)
        }

        for key, values := range request.Form {
            for i, value := range values {
                value = template.HTMLEscapeString(value)
                value = template.JSEscapeString(value)
                request.Form[key][i] = value
            }
        }
        next.ServeHTTP(response, request)
    }
}

Error obtained:

cannot use User.CreateUser (type func(http.ResponseWriter, *http.Request, httprouter.Params)) as type http.Handler in argument to middlewares.EscapeStringsMiddleware:
func(http.ResponseWriter, *http.Request, httprouter.Params) does not implement http.Handler (missing ServeHTTP method)

2.

func EscapeStringsMiddleware(next httprouter.Handle) httprouter.Handle {

    return func (response http.ResponseWriter, request *http.Request, ps httprouter.Params) {
        err := request.ParseForm()
        if err != nil {
            panic(err)
        }

        for key, values := range request.Form {
            for i, value := range values {
                value = template.HTMLEscapeString(value)
                value = template.JSEscapeString(value)
                request.Form[key][i] = value
            }
        }
        next.ServeHTTP(response, request)
    }
}

Error obtained:

next.ServeHTTP undefined (type httprouter.Handle has no field or method ServeHTTP)

Also how do I chain multiple middleware?

For example,

router.POST("/api/user/create", middlewares.VerifyCSRF(middlewares.EscapeStringsMiddleware(User.CreateUser)))
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqphg40600 2017-05-14 13:43
    已采纳

    This issue is not with your middleware handler. You are getting errs because User.CreateUser is not of type http.Handler.

    Try this pattern :

    The important bit is to return a http.Handler and wrap func(w http.ResponseWriter, r *http.Request) with http.HandlerFunc.

    func Handler() http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // do stuff
        })
    }
    

    go source :

    // The HandlerFunc type is an adapter to allow the use of
    // ordinary functions as HTTP handlers. If f is a function
    // with the appropriate signature, HandlerFunc(f) is a
    // Handler that calls f.
    type HandlerFunc func(ResponseWriter, *Request)
    
    // ServeHTTP calls f(w, r).
    func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
        f(w, r)
    }
    

    Based on your feedback:

    httprouter.Handle does not implement ServeHTTP. It is called directly. For example : next(w, r, ps)

    Below you will find examples of middleware handlers.

    // Middleware without "github.com/julienschmidt/httprouter"
    func StdToStdMiddleware(next http.Handler) http.Handler {
    
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // do stuff
            next.ServeHTTP(w, r)
        })
    }
    
    // Middleware for a standard handler returning a "github.com/julienschmidt/httprouter" Handle
    func StdToJulienMiddleware(next http.Handler) httprouter.Handle {
    
        return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
            // do stuff
            next.ServeHTTP(w, r)
        }
    }
    
    // Pure "github.com/julienschmidt/httprouter" middleware
    func JulienToJulienMiddleware(next httprouter.Handle) httprouter.Handle {
    
        return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
            // do stuff
            next(w, r, ps)
        }
    }
    
    func JulienHandler() httprouter.Handle {
        return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
            // do stuff
        }
    }
    
    func StdHandler() http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // do stuff
        })
    }
    
    func main() {
        router := httprouter.New()
        router.POST("/api/user/create", StdToJulienMiddleware(StdHandler()))
        router.GET("/api/user/create", JulienToJulienMiddleware(JulienHandler()))
        log.Fatal(http.ListenAndServe(":8000", router))
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douli0531 2017-12-10 17:08

    From https://github.com/cnvrtly/adaptr lib where adapters are also added to chain request middleware functions.

    func compatibleHandler(h http.Handler, httprParamsCtxKey interface{}) httprouter.Handle {
    return toHttpRouterHandle(h, httprParamsCtxKey)
    }
    func toHttpRouterHandle(h http.Handler, httprParamsCtxKey interface{}) func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
       return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        if httprParamsCtxKey != nil {
            r = SetCtxValue(r, httprParamsCtxKey, p)
        }
        h.ServeHTTP(w, r)
       }
    }
    

    If you are looking for example/idea how to define routes: https://github.com/cnvrtly/golang-appengine-seed

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题