Tech behind Tech

Raw information. No finesse :)

Posts Tagged ‘clojure

Introduction to Clojure Web Development using Ring, Compojure and Sandbar

with 14 comments

I gave an introduction to clojure web development presentation to bay area clojure user group. As my slides do not have much information, I am writing this blog so everyone can follow and get started with a sample clojure web application using compojure.

Setting Up:

 lein new address_book 

Add Compojure to Project:

Edit project.clj

(defproject address_book "1.0.0-SNAPSHOT"
  :description "Runnering log"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [ring/ring-jetty-adapter "0.2.5"]
                 [compojure "0.4.1"]
                 [hiccup "0.2.6"]
                 [sandbar "0.3.0-SNAPSHOT"]
                 [clj-json "0.3.1"]]
  :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]])


lein deps

Test whether our setup works:

Edit src/address_book/core.clj

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]))

(defroutes rts
  (GET "/" [] "Address Book!!")
  (route/not-found "Page not found"))

(def application-routes

(defn start []
  (run-jetty application-routes {:port 8080
                                 :join? false}))

Goto http://localhost:8080 and you should see “Address Book”

Interactive Development:

In core.clj, change start function to

(defn start []
  (run-jetty application-routes {:port 8080
                                 :join? false}))

Add Static Folders:

mkdir -p public/{css,js}

Create public/index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"	"">'
<html xmlns="" xml:lang="en">	
   <meta http-equiv="content-type" content="text/html; charset=utf-8" />		
   <title>My Address book</title>		
   <script type="text/javascript" src=""></script>	
 <body id="main">    
    <p>      My Address Book    </p>  

Try to access http://localhost:8080/index.html. This will fail, as we never told compojure how to handle static files.

Edit core.clj and replace rts

(defroutes rts
  (GET "/" [] "Address Book!!")
   (route/files "/")
  (route/not-found "Page not found"))

Now when you access http://localhost:8080/index.html, it should display correctly.


Three components

1) Handlers:
Handlers are main functions that process a request. We define handlers using defroutes macro.

2) Middleware:
Middleware are functions that could be chained together to process a request. Middleware functions can take any number of arguments, but the spec stats that first argument should be an handler and function should return an handler. An example for middleware is logging all requests that comes to your webserver. Ring and compojure comes with some standard middleware. We will see in next part how to create our own middleware.

3) Adapters:
Adapters are functions could adapt our handler to a web server. We are using jetty adapter to tie our handler to jetty server.

Lets create our Middleware:

Create src/address_book/middleware.clj

(ns address-book.middleware)

(defn- log [msg & vals]
  (let [line (apply format msg vals)]
    (locking System/out (println line))))

(defn wrap-request-logging [handler]
  (fn [{:keys [request-method uri] :as req}]
    (let [resp (handler req)]
      (log "Processing %s %s" request-method uri)

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]))

Edit core.clj application-routes def

(def application-routes
     (-> rts

Now when you access your server, you should see request log messages.

Main sourcecode:

Create address_book/address.clj

(ns address-book.address
  (:import [java.util Date])
  (:refer-clojure :exclude (find create)))

(def STORE (atom {:1 {:id :1 :name "Siva Jagadeesan" :street1 "88 7th" :street2 "#203" :city "Cupertino" :country "USA" :zipsourcecode 98802}}))

(defn to-keyword [num]
  (if-not (keyword? num)
    (keyword (str num))

(defn random-number []
  (to-keyword (.getTime (Date.))))

(defn create [attrs]
  (let [id (random-number)
        new-attrs (merge {:id id} attrs)]
    (swap! STORE merge {id new-attrs})

(defn find-all []
  (vals @STORE))

(defn find [id]
  ((to-keyword id) @STORE))

(defn update [id attrs]
  (let [updated-attrs (merge (find id) attrs)]
    (swap! STORE assoc id updated-attrs)

(defn delete [id]
  (let [old-attrs (find id)]
    (swap! STORE dissoc id)

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]
            [address-book.address :as address]
            [clj-json.core :as json]))

Add this function to core.clj

(defn json-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/json"}
   :body (json/generate-string data)})

Add these routes to core.clj

(GET "/addresses" [] (json-response (address/find-all)))
(GET "/addresses/:id" [id] (json-response (address/find id)))
(POST "/addresses" {params :params}  (json-response (address/create params)))

Replace your public folder with

This folder has needed css and js files.

Now go to http://localhost:8080/index.html and you should see a address book webapp. You can add and view addresses.


Create src/address_book/auth.clj

(ns address-book.auth
  (:use [sandbar.form-authentication ]

(defrecord AuthAdapter []
  (load-user [this username password]
             (cond (= username "example")
                   {:username "example" :password "password" :roles #{:user}}))
  (validate-password [this]
                     (fn [m]
                       (if (= (:password m) "password")
                         (add-validation-error m "Unable to authenticate user.")))))

(defn form-authentication-adapter []
   {:username "Username"
    :password "Password"
    :username-validation-error "You must supply a valid username."
    :password-validation-error "You must supply a password."
    :logout-page "/"}))

Edit core.clj ns form

(ns address-book.core
  (:use [compojure.core]
        [sandbar.form-authentication ]
        [sandbar.validation ])
  (:require [compojure.route :as route]
			[address-book.middleware :as mdw]
            [address-book.address :as address]
            [address-book.auth :as auth]
            [clj-json.core :as json]))

Add this def to core.clj

(def security-policy
  [#".*\.(css|js|png|jpg|gif|ico)$" :any
   #"/login.*" :any
   #"/logout.*" :any
   #"/permission-denied.*" :any
   #"/addresses" :user
   #"/index.html" :user
   #"/" #{:user}])

Add this route to core.clj

 (form-authentication-routes (fn [_ c] (html c)) (auth/form-authentication-adapter))

Change application routes function in core.clj

(def application-routes
     (-> rts
        (with-security security-policy form-authentication)  		 

Now when you access http://localhost:8080/index.html it will take you to login page. You can login using “example” and “password”.

That is it folks. A simple web app using clojure.

I am sure this blog could be improved a lot. Please leave comments I will update this blog with your feedback.

Written by Siva Jagadeesan

January 19, 2011 at 12:36 am

Posted in Clojure

Tagged with , , , ,

Clojure – good coding guidelines

with 6 comments

Naming :

- only lower case letters separated by hyphens except types and protocols should be named in camelcase
– predicates ends with ?
– destructive functions ends with !
– variables that are meant for be re-binding should have earmuffs
– use “_” for names that will be ignored by the code

Code Structure :

- when there is only “then” clause in a conditional statement use “when” instead of “if”
– put all trailing parenthesis in one line
– for dependency use “:only” for “use” and “require”
– use two spaces for indentation
– destructure arg list only when you want to explain the structure of arg to caller otherwise destructure in first let binding

Coding Style:

- try to solve a problem by using function before trying with macro
– be lazy when possible
– use keywords as keys for map entries
– use keyword-first syntax to access properties on objects
– prefer sequence library functions instead of loop/recurr
– only use anonymous functions for short function definitions that fit comfortably on a single line. Otherwise create a private, named function.
– short functions

Written by Siva Jagadeesan

December 9, 2010 at 11:04 pm

Posted in Clojure

Tagged with ,

Clojure defrecord deftype

with 2 comments

There are different ways to create a struct in clojure.

1) Using Clojure built in data structures

user> (def lotr {:title "Book title"
                 :author {
                          :first-name "first"
                          :last-name "last"}})


user> (:title lotr)
"Book title"


user> (-> lotr :author :first-name)

2) Using defrecord

user> (defrecord Book [title author])
user> (defrecord Person [first-name last-name])
user> (def lotr (Book. "Book Title"
                       (Person. "first" "last")))


user> (:title lotr)
"Book title"


user> (-> lotr :author :first-name)

As you can see, accessing map and defrecord is very similar. defrecord provides implementation of a persistent map.

3) Using deftype

PS: bad example ( I will explain why later )

user> (deftype Book [title author])
user> (deftype Person [first-name last-name])
user> (def lotr (Book. "Book Title"
                       (Person. "first" "last")))

Unlike defrecord, deftype does not provide implementation of a persistent map. So when you try to keyword-access , it won’t work


user> (:title lotr)

It provides field access only

user> (.title lotr)
"Book Title"

I mentioned the above example for deftype is a bad example. Let me explain why. In OO, we have two set of classes. One set that defines language constructs like String and other that defines domain like Book. As defrecord provides implementation of a persistent map, it is better to use defrecord for realizing domain. Use deftype for realizing programming constructs.

Written by Siva Jagadeesan

November 15, 2010 at 10:58 am

Posted in Clojure

Tagged with , , , ,

Testing Clojure Code – Awesome “are”

with 2 comments

test-is a great library for testing clojure. Thanks to Philippe, I recently discover “are” in test-is. This is most underrated/underused macro in test-is library.

Lets take an example, where I want to test a function given a date, it will return end of month.

Without are,

(deftest test-end-of-month
        (is (= "2010-01-31 23:59" (end-of-month "2010-01-03 01:12")))
        (is (= "2010-01-31 23:59" (end-of-month "2010-01-03 21:59")))
        (is (= "2010-02-28 23:59" (end-of-month "2011-02-03 13:01")))
        (is (= "2016-02-29 23:59" (end-of-month "2016-02-03 03:01")))
        (is (= "2011-04-30 23:59" (end-of-month "2011-04-03 00:02"))))

With are,

(deftest test-end-of-month
        (are [expected ts] (= expected (end-of-month ts))

             "2010-01-31 23:59" "2010-01-03 01:12"
             "2010-01-31 23:59" "2010-01-03 21:59"
             "2011-02-28 23:59" "2011-02-03 13:01"
             "2016-02-29 23:59" "2016-02-03 03:01"
             "2011-04-30 23:59" "2011-04-03 00:02"))

The version without “are” has, so many duplications. It is difficult to read as it is cluttered. The version with “are” is easy to read, and it is so easy to add more test cases without much duplication.

Thanks Philippe for introducing me to wonders of “are”.

Written by Siva Jagadeesan

October 19, 2010 at 12:55 pm

Posted in Clojure

Tagged with , ,

Calling java from Clojure

leave a comment »


Importing Java Class

In repl, when you want to import one Java class you can do

(import 'java.util.Date)

When you want to import more Java classes from a same package you can do

(import [java.util Date HashMap])
        (:import [java.util Date HashMap]))

Creating Instances

(import 'java.util.Date)

(def today (new Date))

;; or

(def today (Date.))

Calling Java instance methods

user> (import 'java.util.Date)
user> (let [today (Date.)]
        (.getTime today))

Calling Java static methods

user> (System/currentTimeMillis)

Sugar sytax:


You want to write a function that will return UTC Java Calendar object set at a specific time.

user> (import [java.util Calendar TimeZone Date])
user> (defn utc-time [d]
        (let [cal (Calendar/getInstance)]
              (.setTimeZone cal (TimeZone/getTimeZone "UTC"))
              (.setTime cal d)


The let block is ugly. We could use doto to make this code better.

user> (import [java.util Calendar TimeZone Date])	
user> (defn utc-time [d]        
         (doto (Calendar/getInstance)
              (.setTimeZone (TimeZone/getTimeZone "UTC"))
              (.setTime d)))


Dot Dot:

Sometimes in java you want to make calls in chain

Bad way

user> (.length (.getProperty (System/getProperties) ""))

Better way

user> (. (. (System/getProperties) getProperty "") length)

Even Better

user> (..
       (getProperty "")

Avoiding Reflection

By default jvm will be using reflection to identify type. Reflection is slow. But we can give type hints that way jvm does not have to use reflection. For example we can rewrite utc-time with type-hints like this

user> (set! *warn-on-reflection* true)
user> (defn str-length [s] (.length s))
Reflection warning, NO_SOURCE_FILE:1 - reference to field length can't be resolved.
user> (defn str-length [#^String s] (.length s))

Implementing interfaces and extending classes

Let us implement Java Runnable interface

user> (proxy [Runnable] []
        (run []
             (println "running ...")))
#<Object$Runnable$36fc6471 user.proxy$java.lang.Object$Runnable$36fc6471@6dd33544>

In clojure 1.2, you could use reify macro to implement. In fact it is better than using proxy.

user> (reify Runnable
             (run [this]
                  (println "running ...")))
#<user$eval1664$reify__1665 user$eval1664$reify__1665@574f7121>

Written by Siva Jagadeesan

October 11, 2010 at 10:43 pm

Posted in Clojure

Tagged with , ,

Clojure Macros Simplified

with 5 comments

One of the powerful features of clojure ( or any other LISP) is macros. Macros are so powerful that will allow you do things that you can never do in any other language. There is a classic example of implementing “unless” functionality using macros. I am not going to talk about why we need macros and when to use them. There is so much literature out there discussing about Macros. Also checkout Clojure in Action book for in depth look on Macros. I have read both Joy of Clojure and Clojure in Action, when it comes to Macros I will recommend Clojure in Action. The idea behind this blog to simplify macros as much as possible.

As you know in Clojure code is data and data is code. Our code is a clojure List. So we can programmatically create a list and execute it.

user> (def my-code '(println "techbehindtech"))
user> my-code
(println "techbehindtech")
user> (eval my-code)

In above example, we created a list my-code. This list could be created dynamically. And using eval function we could execute that list as a clojure code.

So to create code in clojure, all we have to do is create a clojure list. In clojure, before a code is evaluated we have hook to introduce our own code using Macros system.

In Macros we are basically creating a list of code similar to my-code. Lets create a simple macro that will allow us to write functions with some log message.

(defmacro def-logged-fn [fn-name args & body]
        `(defn ~fn-name ~args
           (println "Calling ...")
user> (def-logged-fn say[name]
        (println (str "hello " name)))
user> (say "siva")
Calling ...
hello siva

macroexpand and macroexpand-1:

macroexpand and macroexpand-1 are useful functions to know about when using
writing macros. These functions expand our macro forms.

user> (macroexpand-1 '(def-logged-fn say[name]
        (println (str "hello " name))))

 (clojure.core/println "Calling ...")
 (println (str "hello " name)))

You can see that our macro created a fn called “say” that calls println first before the original body. That is pretty cool huh.

The difference between macroexpand-1 and macroexpand is macroexpand-1
will expand only one level of macros and macroexpand calls macroexpand-1
until all macro forms are expanded.

The same macro expanded with macroexpand will look like

(macroexpand '(def-logged-fn say[name]
        (println (str "hello " name))))

   (clojure.core/println "Calling ...")
   (println (str "hello " name)))))

As you can see macroexpand expanded defn macro also.

Coming back to our macro, there are 3 reader macros that we have used

- Backtick `
– Tilda ~
– Tilda Ampersand ~@

Backtick ` (syntax-quote):

This is called as syntax-quote. This quotes the whole expression that way you do not have to quote each one of them

Tilda ~ (unquote):

This is called as unquote. This unquotes (substitutes) the value. For
instance, we wanted to replace fn-name with its value, so we tilda
before fn-name.

Tilda Ampersand ~@ (unquote-splicing):

It is easy to show with an example. Lets take our macro that we wrote and change unquote-splicing to unquote before body and let us what happens.

user> (defmacro def-logged-fn [fn-name args & body]
        `(defn ~fn-name ~args
           (println "Calling ...")
user> (macroexpand-1 '(def-logged-fn say[name]
        (println (str "hello " name))))

 (clojure.core/println "Calling ...")
 ((println (str "hello " name))))

You can see the last line looks wierd. It is trying to call output of
println as a function. This will obiviously fail.

user> (def-logged-fn say[name]
         (println (str "hello " name)))
user> (say "techbehindtech")
Calling ...
hello techbehindtech
; Evaluation aborted.

No message.
  [Thrown class java.lang.NullPointerException]

As body is a list ( we are using varible args) when we just unquote it
is putting body inside a single list. To avoid that we have to


Inside a macro, sometimes you want to create unqualified symbol to use
in a let block. We can do this easily by appending # in end of symbol.

Written by Siva Jagadeesan

September 28, 2010 at 4:32 pm

Posted in Clojure

Tagged with , , , ,

Compojure Demystified with an example – Part 6

with one comment

In this part we will start implementing edit and delete functionalities.

We are going to build these services,

Edit address – PUT – http://localhost:8080/addresses/:id
Delete address – DELETE – http://localhost:8080/addresses/:id

1) Adding PUT and DELETE route

Edit routes.clj

(ns address_book.routes
  (:use [compojure.core])
  (:require [address-book.address :as address]
            [address-book.middleware :as mdw]
            [compojure.route :as route]
            [clj-json.core :as json]))

(defn json-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/json"}
   :body (json/generate-string data)})

(defroutes handler
  (GET "/addresses" [] (json-response (address/find-all)))
  (GET "/addresses/:id" [id] (json-response (address/find id)))
  (POST "/addresses" {params :params}  (json-response (address/create params)))
  (PUT "/addresses/:id" {params :params} (json-response (address/update (params "id")  params)))
  (DELETE "/addresses/:id" [id] (json-response (address/delete id )))

  (route/files "/" {:root "public"})
  (route/not-found "Page not found"))

(def address-book
     (-> handler

As you can see it is pretty straight forward to add PUT and DELETE route.

What is up with params?

In previous version of Compojure there were some magic variables, like params, session etc. From version 0.4 there are no more magic variables. Instead Compojure provides a map “params”. “params” map will contain all request information. For some reason, params map have a key :params which contains request parameters. That is why we have do {params :params}. I wish they will rename “params” to something else.

Instead of doing {params :params}, the compojure document states that we could do [params id] and when compojure detects a vector, instead of a map it will assign the parameters to each value of the vector. But for some reason, I was not able to make it work.

Front End code

Checkout from github for front end code. Warning: front end code is bad, do not take this as an example.

In next part we will see how to send flash messages.

Source code is now available at github. Created branches for each part.

Next part is posted.

Written by Siva Jagadeesan

September 16, 2010 at 11:09 pm

Posted in Clojure

Tagged with , , , , ,


Get every new post delivered to your Inbox.

Join 146 other followers

%d bloggers like this: