Rich Hickey has been advocating that services should be highly decoupled with a common, extensible format, such as edn. Fogus has a great library for exposing a REST service with the application/edn content type, and Edmund Jackson has a blog post about transmitting edn data through URLs, but neither example demonstrates a full stack service using edn. There are other full stack alternatives, such as shoreleave, but these abstract away the communication protocol, creating a tighter coupling between the frontend and backend of an application.
This blog post presents a REST service which expects an edn map with a :name key. The service reverses the name and returns it as an edn map containing a :message key.
The full spike with a project.clj file and instructions for testing the spike can be found at github.
Clojure Ring app
The Ring app contains both the REST service and the static files for the frontend.
The :edn-params in the request map is supplied by Fogus’ ring-ednproject, from which the generate-response function is also taken.
One of the most important parts of the above code is setting the Content-Type. Without that, the :edn-params would not catch the data.
The HTML
The HTML itself is pretty straightforward.
Conclusion
This post has demonstrated a minimal example of a REST stack utilising edn with a Clojure backend and a ClojureScript frontend. While it might initially seem like a big task, it is acutally quite simple to get up and running, even without frameworks such as shoreleave.
As mentioned previously, the full example can be found here at github.