Compojure/Getting Started

Build from Source
To download and build Compojure, you will need Git (git), Leiningen (lein), and a Java runtime environment (java).

download latest release from git (ex https://github.com/weavejester/compojure.git) cd compojure lein deps lein jar

This will generate the compojure.jar file. Put this in your Java classpath.

The deps lein target will download the needed dependencies. If this fails, you can manually download them from:

http://cloud.github.com/downloads/weavejester/compojure/deps.zip

Unzip the file in the compojure directory and rerun lein.

Hello World with Embedded Jetty
Create a shell script called "compojure" to run Clojure with compojure.jar and the deps directory in the classpath:

or, for Windows:

Then create a Clojure source file called "hello.clj":

(ns hello-world (:use compojure))

(defroutes greeter (GET "/" (html [:h1 "Hello World"])))

(run-server {:port 8080} "/*" (servlet greeter))

Next, run this file using the compojure shell script:

./compojure hello.clj

Your web application should now be accessible at: http://localhost:8080/

Hello World in a War file
First, create your servlet file:

(ns myapp.MyServlet (:use compojure)  (:gen-class :extends javax.servlet.http.HttpServlet))
 * src/myapp/MyServlet.clj

(defroutes greeter (GET "/" (html [:h1 "Hello world"])))

(defservice greeter)

Next, create your WEB-INF directory:

mkdir -p WEB-INF/lib WEB-INF/classes

Copy compojure.jar, clojure-contrib.jar and clojure.jar to WEB-INF/lib.

You now need to create WEB-INF/web.xml:

Finally, compile the War file. Here is an ant build script that does just that:

A bit more verbose example
Here is an example of a simple calculator.

(ns example  (:use compojure))

(defn html-doc  [title & body]   (html (doctype :html4) [:html [:head [:title title]] [:body [:div [:h2 ;; Pass a map as the first argument to be set as attributes of the element [:a {:href "/"} "Home"]]] body]]))

(def sum-form  (html-doc "Sum" (form-to [:post "/"]      (text-field {:size 3} :x)       "+"       (text-field {:size 3} :y)       (submit-button "="))))

(defn result  [x y]   (let [x (Integer/parseInt x)         y (Integer/parseInt y)] (html-doc "Result"      x " + " y " = " (+ x y))))

(defroutes webservice (GET "/" sum-form)  (POST "/" (result (params :x) (params :y))))

(run-server {:port 8080}  "/*" (servlet webservice))