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:

  |-eCloudEdit  
   |-bin  
   |-config  
   |-doc  
   |-lib  
   |---ece_db  
   |-----doc  
   |-----ebin  
   |-----include  
   |-----src  
   |---ece_web  
   |-----doc  
   |-----ebin  
   |-----include  
   |-----priv  
   |-------images  
   |-------javascripts  
   |---------controllers  
   |---------models  
   |---------views  
   |-------stylesheets  
   |-----src

Under eceweb/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 eceweb_sup and how the supervisor loads Webmachine:

[

-spec init(list()) -> {ok, {SupFlags::any(), [ChildSpec::any()]}} |  
                          ignore | {error, Reason::any()}.  
init([]) ->  
    WebChild = {webmachine_mochiweb,  
                {webmachine_mochiweb, start, [config()]},  
                permanent, 5000, worker, dynamic},  

    RestartStrategy = one_for_one,  
    MaxRestarts = 3,  
    MaxSecondsBetweenRestarts = 10,  
    SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},  

    {ok, {SupFlags , [WebChild]}}.  

config() ->  
    {ok, IP} = application:get_env(webmachine_ip),  
    {ok, Port} = application:get_env(webmachine_port),  
    {ok, App}= application:get_application(),  
    LogDir = code:priv_dir(App) ++ "/logs",  
    {ok, Dispatch} = file:consult(filename:join([code:priv_dir(App), "dispatch"])),  

    [{ip, IP},  
     {port, Port},  
     {log_dir, LogDir},  
     {backlog, 128},  
     {dispatch, Dispatch}].

](https://github.com/tsloughter/eCloudEdit/blob/master/lib/eceweb/src/eceweb_sup.erl)

The dispatch terms are loaded from a file dispatch in the application's priv directory. For this app the dispatch file contains:

[

{["documents", id], ece_resource_documents, []}.  
{["documents"], ece_resource_documents, []}.  
{['*'], ece_resource_static, []}.

](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.

[

-record(ctx, {db}).  

init([]) ->  
    {ok, PID} = ece_db_sup:start_child(),  
    {ok, #ctx{db=PID}}.  

allowed_methods(ReqData, Ctx) ->  
    {['HEAD', 'GET', 'POST', 'PUT'], ReqData, Ctx}.  

content_types_accepted(ReqData, Ctx) ->  
    {[{"application/json", from_json}], ReqData, Ctx}.  

content_types_provided(ReqData, Ctx) ->  
    {[{"application/json", to_json}], ReqData, Ctx}.  

finish_request(ReqData, Ctx) ->  
    ece_db_sup:terminate_child(Ctx#ctx.db),  
    {true, ReqData, Ctx}.

](https://github.com/tsloughter/eCloudEdit/blob/master/lib/eceweb/src/eceresource_documents.erl)

To handle GET requests we implemented the tojson_ function we specified in contenttypesprovided to handle GET requests for json data.

[

to_json(ReqData, Ctx) ->  
    case wrq:path_info(id, ReqData) of  
        undefined ->  
            All = ece_db:all(Ctx#ctx.db),  
            {All, ReqData, Ctx};  
        ID ->  
            JsonDoc = ece_db:find(Ctx#ctx.db, ID),  
            {JsonDoc, ReqData, Ctx}  
    end.

](https://github.com/tsloughter/eCloudEdit/blob/master/lib/eceweb/src/eceresource_documents.erl)

wrq:pathinfo_ 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 ecedb:all/1_ and ecedb: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.

[

process_post(ReqData, Ctx) ->  
    [{JsonDoc, _}] = mochiweb_util:parse_qs(wrq:req_body(ReqData)),  
    {struct, Doc} = mochijson2:decode(JsonDoc),  
    NewDoc = ece_db:create(Ctx#ctx.db, {Doc}),  
    ReqData2 = wrq:set_resp_body(NewDoc, ReqData),  
    {true, ReqData2, Ctx}.

](https://github.com/tsloughter/eCloudEdit/blob/master/lib/eceweb/src/eceresource_documents.erl)

wrq:reqbody/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 ecedb_ 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:setrespbody/2

Lastly, updating documents requires support for PUT. In contentstypeaccepted/2 we've specified that PUT requests with JSON content is sent to the function fromjson/2_:

[

from_json(ReqData, Ctx) ->  
    case wrq:path_info(id, ReqData) of  
        undefined ->  
            {false, ReqData, Ctx};  
        ID ->  
            JsonDoc = wrq:req_body(ReqData),  
            {struct, Doc} = mochijson2:decode(JsonDoc),  
            NewDoc = ece_db:update(Ctx#ctx.db, ID, Doc),  
            ReqData2 = wrq:set_resp_body(NewDoc, ReqData),  
            {true, ReqData2, Ctx}  
    end.

](https://github.com/tsloughter/eCloudEdit/blob/master/lib/eceweb/src/eceresource_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 ecedb's _update/2 function.

In the next post I'll show how ecedb_ is implemented with Couchbeam for reading and writing the documents to CouchDB on Cloudant.