Negroni和Gorilla Context ClearHandler

huangapple go评论99阅读模式
英文:

Negroni and Gorilla Context ClearHandler

问题

是否可以像在Alice中将Gorilla的 context.ClearHandler()用作Negroni的中间件一样使用?类似于:

n.Use(context.ClearHandler())

目前,我在每个响应之后都调用context.Clear(r),但我希望自动处理清理工作。我目前遇到以下错误:

cannot use context.ClearHandler() (type http.Handler) as type negroni.Handler in argument to n.Use:                                                                   
http.Handler does not implement negroni.Handler (wrong type for ServeHTTP method)                                                                                                  
  have ServeHTTP(http.ResponseWriter, *http.Request)                                                                                                                         
  want ServeHTTP(http.ResponseWriter, *http.Request, http.HandlerFunc)

但我不确定错误消息告诉我什么。

英文:

Is is possible to use Gorilla's context.ClearHandler() as middleware for Negroni like I've seen it used as middleware for Alice? Something like:

n.Use(context.ClearHandler())

At the moment I'm calling context.Clear(r) after every response but I would prefer the tidying up to be taken care of automatically. I'm currently getting the following error:

cannot use context.ClearHandler() (type http.Handler) as type negroni.Handler in argument to n.Use:                                                                   
http.Handler does not implement negroni.Handler (wrong type for ServeHTTP method)                                                                                                  
  have ServeHTTP(http.ResponseWriter, *http.Request)                                                                                                                         
  want ServeHTTP(http.ResponseWriter, *http.Request, http.HandlerFunc)

But I'm not sure what the error message is telling me.

答案1

得分: 4

Negroni.Use() 期望一个 negroni.Handler 类型的参数,但 Gorilla 的 context.ClearHandler() 返回的是一个 http.Handler 类型的值。

好消息是,有一个替代方法 Negroni.UseHandler(),它期望一个 http.Handler,所以只需使用它即可。请注意,context.ClearHandler() 也期望另一个 http.Handler

otherHandler := ... // 要清除的其他处理程序
n.UseHandler(context.ClearHandler(otherHandler))

注意:

gorilla/mux 包中的 Router 在请求生命周期结束时自动调用 context.Clear(),所以如果你在使用它,就不需要使用 context.ClearHandler() 清除上下文。你只需要在其他/自定义处理程序中使用它(除非你想手动调用 context.Clear())。

英文:

Negroni.Use() expects a parameter of type negroni.Handler but Gorilla's context.ClearHandler() returns a value of type http.Handler.

Good thing is that there is an alternative Negroni.UseHandler() method which expects an http.Handler so just use that. Note that context.ClearHandler() also expects another http.Handler:

otherHandler := ... // Other handler you want to clear
n.UseHandler(context.ClearHandler(otherHandler))

Notes:

The Router from the gorilla/mux package automatically calls context.Clear() at the end of a request lifetime, so if you are using it you don't need to clear the context using context.ClearHandler(). You only need to use it for other / custom handlers (unless you want to call context.Clear() manually).

huangapple
  • 本文由 发表于 2015年5月28日 20:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/30506831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定