<story><title>Writing HTTP Middleware in Go</title><url>http://justinas.org/writing-http-middleware-in-go/</url><text></text></story><parent_chain><item><author>optymizer</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;  &amp;gt; a painless and even fun process
&lt;/code&gt;&lt;/pre&gt;
This claim might be a little too optimistic. I&amp;#x27;d like to challenge you (everyone) to associate a User object with an http.Request. Example code in Node.js&amp;#x2F;ExpressJS:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  function auth(req, res, next) {
    req.User = User(&amp;quot;username&amp;quot;);
    next();
  }
&lt;/code&gt;&lt;/pre&gt;
This is such a simple example of a useful piece of middleware, yet it is so difficult to implement in Go in a &amp;#x27;painless and fun&amp;#x27; way.&lt;p&gt;I would prefer to avoid contention on a global mutex.&lt;p&gt;I would prefer to keep the compatibility with http.Handler and http.HandlerFunc.&lt;p&gt;Good luck!</text></item></parent_chain><comment><author>tptacek</author><text>Maybe I&amp;#x27;m missing something about your question, but I think my solution to this problem is simply to use closures; if there&amp;#x27;s per-request state (a user, a session, a data store), I write a function that returns an anonymous HandlerFunc that wraps up the real handler, sets up its environment, and passes it down.&lt;p&gt;This is &amp;quot;compatible&amp;quot; with http.Handler and http.HandlerFunc; everything is wired together as if it was vanilla handlers. But my actual handlers (or &amp;quot;actions&amp;quot; or whatever you&amp;#x27;d like to call them) get state, persistence, &amp;amp;c.</text></comment>
<story><title>Writing HTTP Middleware in Go</title><url>http://justinas.org/writing-http-middleware-in-go/</url><text></text></story><parent_chain><item><author>optymizer</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;  &amp;gt; a painless and even fun process
&lt;/code&gt;&lt;/pre&gt;
This claim might be a little too optimistic. I&amp;#x27;d like to challenge you (everyone) to associate a User object with an http.Request. Example code in Node.js&amp;#x2F;ExpressJS:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  function auth(req, res, next) {
    req.User = User(&amp;quot;username&amp;quot;);
    next();
  }
&lt;/code&gt;&lt;/pre&gt;
This is such a simple example of a useful piece of middleware, yet it is so difficult to implement in Go in a &amp;#x27;painless and fun&amp;#x27; way.&lt;p&gt;I would prefer to avoid contention on a global mutex.&lt;p&gt;I would prefer to keep the compatibility with http.Handler and http.HandlerFunc.&lt;p&gt;Good luck!</text></item></parent_chain><comment><author>elithrar</author><text>To be fair, Express is a web framework. Go&amp;#x27;s net&amp;#x2F;http package is a little lower level than that.&lt;p&gt;Still, this is possible if you make up for the lack of a framework by writing some code yourself. I&amp;#x27;d use (and I &lt;i&gt;do&lt;/i&gt; use) gorilla&amp;#x2F;context[1] to allow me to pass data in the request context between middleware layers (i.e. CSRF middleware passes the token along).&lt;p&gt;You could write a couple of little helpers using gorilla&amp;#x2F;context (SetUser&amp;#x2F;GetUser, or more generically, SetString&amp;#x2F;GetString) and SetUser(username, r) in your middleware. The helpers (see the context docs!) just wrap a type assertion on the Get side, and set a map value on the Set side.&lt;p&gt;&lt;pre&gt;&lt;code&gt;   &amp;#x2F;&amp;#x2F; Compatible with http.HandlerFunc
   func auth(h http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
                SetUser(r, &amp;quot;dave&amp;quot;)
                h.ServeHTTP(w, r)
        }
   }

   &amp;#x2F;&amp;#x2F; Routes
   r.HandleFunc(&amp;quot;&amp;#x2F;admin&amp;quot;, use(adminHandler, auth)
   http.Handle(&amp;quot;&amp;#x2F;&amp;quot;, r)

   &amp;#x2F;&amp;#x2F; Allows you to chain your middleware rather than wrapping functions
   func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
    for _, m := range middleware {
        h = m(h)
    }

    return h
   }
&lt;/code&gt;&lt;/pre&gt;
You can mod this to work with http.Handler if you want: replace HandlerFunc and then write r.Handle(&amp;quot;&amp;#x2F;admin&amp;quot;, use(http.HandlerFunc(adminHandler), auth) instead.&lt;p&gt;[1]: &lt;a href=&quot;http://www.gorillatoolkit.org/pkg/context&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;www.gorillatoolkit.org&amp;#x2F;pkg&amp;#x2F;context&lt;/a&gt;</text></comment>