Skip to content

Instantly share code, notes, and snippets.

@kolov
Last active August 29, 2015 14:03
Show Gist options
  • Save kolov/84cafcb5557fb6cd59bb to your computer and use it in GitHub Desktop.
Save kolov/84cafcb5557fb6cd59bb to your computer and use it in GitHub Desktop.
Ring middleware to bind request and response to variables easy to explore in the repl
(declare dbg-req)
(declare dbg-resp)
(defn bind-req-resp [handler re-pattern]
(fn [req]
(if (re-matches re-pattern (:uri req))
(do
(alter-var-root (var dbg-req) (constantly req))
(let [resp (handler req)]
(alter-var-root (var dbg-resp) (constantly resp))
resp))
(handler req)
)))
@kolov
Copy link
Author

kolov commented Jun 28, 2014

Example usage:

(def my-app (-> routes
                     (bind-req-resp #"/service/.*")              
                     (ring.middleware.reload/wrap-reload)                  
                     (friend/authenticate auth-settings)                 
                     compojure.handler/site))

Then, after a call to /service/xxx, in the namespace of the bind-req-resp:

my_app=>(clojure.pprint/pprint (keys dbg-req))
(:ssl-client-cert
 :cookies
 :remote-addr
 :params
 :flash
 :headers
 :server-port
 :content-length
 :form-params
 :civi.cookie/user
 :session/key
 :query-params
 :content-type
 :character-encoding
 :uri
 :server-name
 :query-string
 :body
 :multipart-params
 :scheme
 :cemerick.friend/auth-config
 :request-method
 :session
 :model)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment