gonegroni

Passing arguments to Negroni middleware


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

Solution

  • 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"))