Clojure on Heroku with Noir and Mongo in 10 minutes
Heroku is a easy-to-use cloud deployment platform. It allows you to use git to deploy and launch a webapp in a few minutes. Recently, they've provided support for Clojure on their stack, and I wanted to show how you can use Noir, a Clojure webapp framework, to easily deploy to Heroku. This allows you to experiment with Clojure in production without an up-front investment. This exercise will take about 10 minutes. First, you need to sign up for a free account (I don't get paid for this). For this demo you also need to install lein, the Clojure package management tool installed, and the heroku gem. Here's how to do that:
Once you have the dependencies, let's get a simple app running on Heroku. Just execute the following few commands.
Execute these, and you should get a message from Heroku with the url of your app. Verify that your app is displaying the Noir welcome screen. It might take a second for it to become available.
Let's add mongo so that we can save data somewhere. You can initialize mongo on heroku with the command line tool: Next, we need to do something that writes to mongo. To do that, we need to use a mongo library for Clojure. We can add a dependency by modifying the project.clj file in your project's root directory. Specifically, we add the "congomongo" library to the dependencies, as so:
Now we need to write some code to use mongo. Here is some code that you can put in the /welcome view (in src/noir_mongo_heroku/views/welcome.clj), which connects to mongo and updates a counter. If you're still in the project's root dir, you can download this file by running the command below. Here's a simple wget command to modify your /welcome view. Run it from the project's root dir.
Now update the app on heroku. You should be able to browse to your app's /welcome url, and with each reload of the page, see an updating counter that proves that it's actually writing to mongo. You can find out details about your mongo installation (including the URL, collections, and shell command) on your Heroku app page.
I'm very happy to welcome Chris Granger, the author of Noir, to ReadyForZero. We're excited to have him working on Clojure development here. We'll hope he'll continue to improve Noir and help us release other great open source software. We're looking for a few more great Clojure and Python developers, you should apply right now, or complete our programming puzzles.