Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the "big idea" behind compojure routes?
    text
    copied!<p>I'm new to Clojure and have been using Compojure to write a basic web application. I'm hitting a wall with Compojure's <code>defroutes</code> syntax, though, and I think I need to understand both the "how" and the "why" behind it all.</p> <p>It seems like a Ring-style application begins with an HTTP request map, then just passes the request through a series of middleware functions until it gets transformed into a response map, which gets sent back to the browser. This style seems too "low level" for developers, thus the need for a tool like Compojure. I can see this need for more abstractions in other software ecosystems as well, most notably with Python's WSGI.</p> <p>The problem is that I don't understand Compojure's approach. Let's take the following <code>defroutes</code> S-expression:</p> <pre><code>(defroutes main-routes (GET "/" [] (workbench)) (POST "/save" {form-params :form-params} (str form-params)) (GET "/test" [&amp; more] (str "&lt;pre&gt;" more "&lt;/pre&gt;")) (GET ["/:filename" :filename #".*"] [filename] (response/file-response filename {:root "./static"})) (ANY "*" [] "&lt;h1&gt;Page not found.&lt;/h1&gt;")) </code></pre> <p>I know that the key to understanding all of this lies within some macro voodoo, but I don't totally understand macros (yet). I've stared at the <code>defroutes</code> source for a long time, but just don't get it! What's going on here? Understanding the "big idea" will probably help me answer these specific questions:</p> <ol> <li>How do I access the Ring environment from within a routed function (e.g. the <code>workbench</code> function)? For example, say I wanted to access the HTTP_ACCEPT headers or some other part of the request/middleware?</li> <li>What's the deal with the destructuring (<code>{form-params :form-params}</code>)? What keywords are available for me when destructuring?</li> </ol> <p>I really like Clojure but I am so stumped!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload