Sunday, October 02, 2011

Experimenting with Clojure 1.3 and Noir 1.2

Noir is a Clojure "mini framework" that is built on top of Compojure. Chris Granger released a new version today that is updated for Clojure 1.3. After working mostly in Clojure last year but using Clojure not very much this year (lots of work for a Java shop) I decided to check out both Clojure 1.3 and Noir 1.2 this afternoon - and I liked what I saw.

The Noir example application uses a recent version of clj-stacktrace and stack traces are much better: Noir prints a well formatted stack trace on any generated web page if an error occurs. This stack trace is very good, filtering out information that you really don't want to see, identifying where the error occurred, and with usually a useful error message.

This eliminates the only major complaint I have ever had with Clojure. Very cool!

The Noir web site had a link to an article written by Ignacio Thayer on running a Clojure Noir MongoDB app on Heroku, using a free MongoDB account. Worked great. I made a trivial change to src/noir_mongo_heroku/views/welcome.clj to also work with a local MongoDB service:
 (let [mongo-url (get (System/getenv) "MONGOHQ_URL")]
   (if mongo-url
     (let [config    (split-mongo-url mongo-url)]
       (mongo! :db (:db config)
               :host (:host config)
               :port (Integer. (:port config)))
       (authenticate (:user config) (:pass config)))
     (mongo! :db "db"))
Noir roughly supplies the same general level of functionality as Sinatra. Noir's development environment, like Compojure that it is layered on, supports live code reloading so if you are used to an interactive dev style like that of Rails and Sinatra, and if you like Lisp (:-) then give it a try.

I am an old Lisp hacker, starting with Lisp on a Dec 10, and getting my first Lisp Machine in 1982. I think that Lisp (Common Lisp, Scheme, Clojure, etc.) mostly appeals to those of us who would (mostly) like to build up our own infrastructure. I say this even though some Lisps have huge libraries: Franz Lisp, Racket, Clojure (both good native libraries and stuff inherited from Java-land), etc. are certainly "batteries included" languages/platforms, but I still characterize Lisp'ers as build it ourselves types.


2 comments:

clem said...

Appreciate the post. I've been experimenting with noir myself, but I'm not sure how the whole package is intended to come together.

For instance, the MongoDB code you wrote is something I would associate with the model rather than the view. I see that the example code generated by Noir includes a model directory, so does the framework support defining storage in the model and then manipulating it as a hash in the view?

Mark Watson, author and consultant said...

Hello Clem,

Ignacio Thayer wrote the code in his article - I just changed a line or two.

You are right that in a real application, it would be good to separate the models and "business logic" code from the views.