Sunday, April 7, 2013

Clojure & Compojure for Web Development

Prompted by Chas Emerick's video on web development with Clojure, I decided to give it a try. I did not use Eclipse as Chas did. I used an editor and the command line. It was easy thanks to Leiningen, a project builder. It is very neat.

My little application has an HTML page http://localhost:3000/hello.html which accepts a name, submits a form and the application returns the name. The application also accepts a URL with a query parameter http://localhost:3000/?name=Greg and echoes the value on the page.

After creating the project with Leiningen with these commands,
[clojure] lein new compojure hello-world
[clojure] cd hello-world

update the source code Leiningen has created with the files below and then run the command to start the server:
[hello-world] lein ring server

file: ./project.clj file: ./src/hello-world/handler.clj ./resources/public/hello.html Why Use Clojure? Clojure is a Lisp with many great, newly added features but the basic features of Lisp may be its best selling point. Here's what many Lisp developer think about their language: Greenspun's tenth rule states: Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.