title: Middleware for per-request data
body: <p>In <code>clojure</code>, I can write something like this:</p>
  
  <pre><code>(defn wrap-my-header
    [handler]
    (fn [request]
      (let [request (if (get-in request [:headers "my-header"])
                      (assoc request :has-my-header? true)
                      request)]
        (handler request))))
  </code></pre>
  
  <p>In this middleware, I'm checking if I have a non-nil value in <code>my-header</code> in <code>:headers</code>, if yes, I'll attach some data in the <code>request</code> map. This demonstrates that I can treat <code>request</code> and <code>response</code> as a somewhat "stateful" data.</p>
  
  <p>I'm still new in haskell and wanted to do similar things with <code>scotty</code>. After looking at the type of <a href="http://hackage.haskell.org/package/wai-3.2.1.2/docs/Network-Wai.html#t:Middleware" rel="noreferrer">middleware</a>, I can create a middleware like this:</p>
  
  <pre><code>myMiddleware :: Middleware 
  myMiddleware app req respond = app req respond
  </code></pre>
  
  <p>After staring at the type for a long time, I still don't know  how to do it. Some reading and thinking makes me assume that this is not possible, <code>Middleware</code> can only only short-circuit the handler and/or alter the generated response. Is this true?</p>
  
  <hr>