将参数传递给Negroni中间件

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

Passing arguments to Negroni middleware

问题

每个对我的应用程序的请求都应该使用一些中间件。根据Negroni文档,我已经这样实现了:

func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  // 在之前做一些事情
  next(rw, r)
}

在其他地方,我使用了:

n.Use(negroni.HandlerFunc(MyMiddleware))

然而,这个中间件需要接收一个额外的参数,我不确定如何构建它。目前,我只是将该值设置为全局变量,以便使其可用,但我相信有一种更优雅的方式?

我希望能够像这样做:

n.Use(negroni.HandlerFunc(MyMiddleware(val)))
英文:

Every request to my application should use some middleware. Using the Negroni docs I have implemented it like so:

func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  // do some stuff before
  next(rw, r)
}

and elsewhere I use:

n.Use(negroni.HandlerFunc(MyMiddleware))

However, the middleware needs to receive an extra argument and I'm not sure how to build this in. At the moment I'm just setting the value as a global variable to make it available but I'm sure there's a more elegant way?

I would like to be able to do something like this:

n.Use(negroni.HandlerFunc(MyMiddleware(val)))

答案1

得分: 5

最好的方法是将你的中间件封装为一个包含状态的结构体,而不仅仅是一个无状态函数。(你也可以将其封装为闭包,但我认为结构体更清晰):

type MyMiddleware struct {
    someval string
}

func NewMyMiddleware(someval string) *MyMiddleware {
    return &MyMiddleware{
       someval: someval,
    }
}


func (m *MyMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

    // 使用 someval 做一些操作
    fmt.Println(m.someval)

    next(w, req)
}

初始化它很简单:

n.Use(NewMyMiddleware("foo"))

编辑:也许闭包实际上更简单:

someval := "foo"

n.Use(negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
   // 使用 someval 做一些操作
   fmt.Println(someval)
    
   next(w, req)
}))

或者你可以有一个返回中间件函数的函数:

func NewMiddleware(someval string) negroni.HandlerFunc {
     return negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
       // 使用 someval 做一些操作
       fmt.Println(someval)
        
       next(w, req)
    })
}

然后使用

```go
n.Use(NewMiddleware("foo"))
英文:

The best way would be to encapsulate your middleware as a struct that holds its state, not just a stateless function. (You could also wrap it as a closure but a struct is cleaner IMO):

type MyMiddleware struct {
    someval string
}

func NewMyMiddleware(someval string) *MyMiddleware {
    return &MyMiddleware{
       someval: someval,
    }
}


func (m *MyMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

    // Do Something with someval
    fmt.Println(m.someval)

    next(w, req)
}

and initializing it is simply:

n.Use(NewMyMiddleware("foo"))

EDIT: Perhaps a closure would actually be simple:

 someval := foo

 n.Use(negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
   // Do Something with someval
   fmt.Println(someval)
    
   next(w, req)
}))

Or you could have a function that returns a middleware function:

func NewMiddleware(someval string) negroni.HandlerFunc {
     return negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
       // Do Something with someval
       fmt.Println(someval)
        
       next(w, req)
    })
}

and then

n.Use(NewMiddleware("foo"))

huangapple
  • 本文由 发表于 2015年8月27日 03:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/32234714.html
匿名

发表评论

匿名网友

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

确定