A Simple Web Application with Clojure and CouchDB

By vijay on July 18, 2011 — 2 mins read

Here’s another very small step in my pursuit of learning Clojure. A couple of weeks ago Heroku introduced their new Cedar stack which adds Clojure support on their platform. With my usual curiosity for all things Clojury, I checked out their documentation on how to setup a simple web application written using Compojure and uses PostgreSQL as backend.

I succeeded in following the tutorial and made a simple working prototype even with my limited intelligence. But PostgreSQL seemed old-skool, since all the new cool kids are using NoSQL. So I started changing the backend to use CouchDB instead of PostgreSQL. So with great anticipation of criticism of my Clojure & CouchDB skills, I present how I did it.

The code I used as the starting point is the Shouter app which is explained in much detail in the Heroku’s documentation here. It is a “twitter-clone” (note the quotes!), which is built using Compojure and Hiccup. The code follows clear MVC pattern which made my task of switching the backend extremly easy.

Before I explain what I did in the code, first I need to give a heads-up on pre-requisites, there’s only one if you have a Clojure development installed, and that is CouchDB. Since we are planning to switch the backend to CouchDB the most important thing I needed was – CouchDB! So I went to the Couchbase site and downloaded the super-awesomely-quick-one-click Couchbase Server. This is just a small app you need to run which sits silently in your menubar running the CouchDB Server in the background. Very cool.

Once the couchbase server is ready, next step is to setup the project using lein using the command lein new couch-shouter. Once lein command finishes its business and creates the project, open the project.clj in Emacs. We need to add the required dependencies: clutch – for connecting to CouchDB, compojure & ring – to run the server, hiccup – for HTML generation. Here’s the project.clj in its entirety:

Once the dependencies are added, I had to make sure that they are loaded into my local lib directory by using the lein deps command. The core.clj file is the starting point of the application which provides the routes and contains the function that starts the jetty server.

Here’s the core.clj – the core of the web application

The source for the Controller and the Views are pretty much copies of the original shouter code. Except that I’ve removed the CSS and other class declaration for simplicity. Here’s the shouts.clj controller:

And here’s the listing of the views/layout.clj and views/shouts.clj

And now lets get to the nub of the matter, the model layer. First up is the model base, which provides the connection to the CouchDB Database:

As you can see above, the database-resource function is parsing the URI for the host, username, password, and other details needed for connecting to the database. There’s no username or password information yet, but if you are deploying this app to Heroku with Cloudant then you’ll get this information from the CLOUDANT_URL environment variable.

Anyway, let me show you how the shout model has been changed.

That’s it and the shouter app now runs with CouchDB. The source code of the app is now on Github at couch-shouter project.

That’s all for now and come back soon for the next experiment.

Posted in: Programming

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.