gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:49:10 29 4
gpt4 key购买 nike

使用 julienschmidt/httprouter 链接中间件的最佳方式是什么?

据我搜索,http.HandlerFunc 仅接受 func (w ResponseWriter, r *Request) 形式的函数,而 httprouter.Handle 函数的形式为 func (w http.ResponseWriter, r *http.Request, ps httprouter.Params)

如何在不将 httprouter.Handle 函数转换为 http.HandlerFunc 的情况下链接中间件?

例如:
我的 routes.go 是这样的形式,

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

如何为上述路由编写中间件函数?

已经尝试过的方法:

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)
}
}

获得错误:

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)
}
}

获得错误:

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

另外,我如何链接多个中间件?

例如,

router.POST("/api/user/create", middlewares.VerifyCSRF(middlewares.EscapeStringsMiddleware(User.CreateUser)))

最佳答案

此问题与您的中间件处理程序无关。您会出错,因为 User.CreateUser 不是 http.Handler 类型。

试试这个模式:

重要的一点是返回一个 http.Handler 并用 http.HandlerFunc< 包装 func(w http.ResponseWriter, r *http.Request)/.

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

去源码:

// 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)
}

根据您的反馈:

httprouter.Handle 没有实现 ServeHTTP。它被直接调用。例如:next(w, r, ps)

您将在下面找到中间件处理程序的示例。

// 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))
}

关于go - 在goLang中使用julienschmidt/httprouter时如何使用中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43964461/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com