eCloudEdit: Erlang, Webmachine and Backbone.js
To experiment with using a pure client-side rendering talking to an Erlang backend I’ve taken James Yu’s CloudEdit tutorial an app written with Backbone.js and Rails and converted the backend to use Webmachine and CouchDB. You can see eCloudEdit in action here. The Backbone.js code is the same so to understand that please see James' post, here I’ll describe the Erlang backend.
To begin with we setup two applications, one for handling the web interaction and a second for handling the database interaction. We end up with this directory layout:
Under ece_web/priv is where all the html and Javascript from CloudEdit is placed, with nothing changed. To serve up the static content through Webmachine we use this resource which we won’t worry about in this article. Lets look at how webmachine deciced where to route requests. This is handled by a set of dispatch rules that are passed into Webmachine on startup. We can see how this is done by looking at ece_web_sup and how the supervisor loads Webmachine:
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/src/ece_web_sup.erl)
The dispatch terms are loaded from a file dispatch in the application’s priv directory. For this app the dispatch file contains:
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/priv/dispatch)
There are two resources, one for handling the requests for creating, updating and viewing documents and one for serving up all other requests (static html, js and css files). The first rule matches paths like, /documents/foo, id is set to foo and the request is sent to the documents resource. If there is nothing after /documents it is still sent to the documents resource but there is no id.
Webmachine is essentially a REST toolkit. You build resources by defining functions that handle the different possible HTTP requests. For this webapp we’ll only be dealing with GET, POST and PUT. GET is used if we’d like to retrieve information on documents, POST is for creating a new document and PUT is for updating a document.
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/src/ece_resource_documents.erl)
To handle GET requests we implemented the to_json function we specified in content_types_provided to handle GET requests for json data.
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/src/ece_resource_documents.erl)
wrq:path_info is used to find the value of id, which we set in the dispatch file, if it is undefined we know the request is for all documents, while if it has a value we know to find the contents of the document with that id and return its contents. We’ll see the content of ece_db:all/1 and ece_db:find/2 in the next article. Just know they both return JSON data structures.
Now we must support POST for creating documents or we have nothing to return to a GET request.
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/src/ece_resource_documents.erl)
wrq:req_body/1 returns the contents of the body sent in the HTTP request. Here it is the conents of the document to store. We decode it to an Erlang data structure and pass it to the ece_db app for inserting into the database. After inserting to the database the create/2 function returns the new document with a new element id (in this case generated by CouchDB). This is required so we know the document’s id which is used by the Backbone.js frontend. In order to return it from the POST request we must set response body to the contents of the document with wrq:set_resp_body/2
Lastly, updating documents requires support for PUT. In contents_type_accepted/2 we’ve specified that PUT requests with JSON content is sent to the function from_json/2:
[
](https://github.com/tsloughter/eCloudEdit/blob/master/lib/ece_web/src/ece_resource_documents.erl)
If this request was not routed through the first rule in our dispatch file it does not have an id and thus can not be an update. When this happens we return false so the frontend is aware something has gone wrong. For requests containing an id we pass the contents of the requests body to ece_db’s update/2 function.
In the next post I’ll show how ece_db is implemented with Couchbeam for reading and writing the documents to CouchDB on Cloudant.