justinas / alice

Painless middleware chaining for Go

Home Page:https://godoc.org/github.com/justinas/alice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A way of passing variables between middlewares

lindemannrichard opened this issue · comments

Which way of passing values between middlewares do you suggest?

In case you do not want to have a global vars (e.g.. gorilla-*) but want something specific to the request itself.

Imaging that you have middleware which extracts a lot of data from request and store then in some context struct for down the road middlewares to use this info to perform activities.
(simple example is Authentication or Session info)

e.g.: goji passes a goji.C (a context) struct down the chain.

Alice tries not to get in the way: it is only a helper and does not change the existing interfaces (net/http). Unfortunately, net/http provides no support for passing data between the handlers.

Using purely net/http interfaces, I see no way to deal with this without relying on a global store.

Note that gorilla/context or similar tools are exactly for storing data "specific to the reqest itself". gorilla/context's underlying store is a map[*http.Request]map[interface{}]interface{}, so data from separate requests will never get mixed.