Monday, 14 January 2013

clojure REST API 101

Compojure by James Reeves(a British developer) is a small routing library for Ring that allows web applications to be composed of small, independent parts. (Compojure Github, 2012)

Ring is a Clojure web applications library inspired by Python's WSGI and Ruby's Rack.

                                                                    Fig. a Rack app

Why Compojure?
The language I work with from day to day is Ruby, so Compojure has a  lot in common with lightweight Ruby frameworks like Sinatra, and less  with existing Java frameworks. It's designed for impatient, lazy  people like myself, so it's quick to install and quick to start  developing in.

- @James Reeves (on Google Group)



After installing lein, the following are the steps to get started with Clojure on Compojure :

STEP 1 install lein-newnew 0.2.6 and compojure/lein-template 0.2.0 

prayag@Prayag:~$ lein plugin install lein-newnew 0.2.6
[INFO] Unable to find resource 'lein-newnew:lein-newnew:jar:0.2.6' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'stencil:stencil:pom:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: org/clojure/clojure/1.3.0/clojure-1.3.0.pom from repository clojars at http://clojars.org/repo/
Transferring 5K from clojars
Downloading: slingshot/slingshot/0.8.0/slingshot-0.8.0.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'bultitude:bultitude:pom:0.1.5' in repository central (http://repo1.maven.org/maven2)
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: slingshot/slingshot/0.8.0/slingshot-0.8.0.jar from repository clojars at http://clojars.org/repo/
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.jar from repository central at http://repo1.maven.org/maven2
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.jar from repository central at http://repo1.maven.org/maven2
Downloading: org/clojure/clojure/1.3.0/clojure-1.3.0.jar from repository clojars at http://clojars.org/repo/
Transferring 6K from clojars
Unable to locate resource in repository
[INFO] Unable to find resource 'bultitude:bultitude:jar:0.1.5' in repository central (http://repo1.maven.org/maven2)
Downloading: bultitude/bultitude/0.1.5/bultitude-0.1.5.jar from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'stencil:stencil:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: stencil/stencil/0.2.0/stencil-0.2.0.jar from repository clojars at http://clojars.org/repo/
Transferring 3K from clojars
Transferring 16K from clojars
Transferring 3311K from clojars
Copying 3 files to /tmp/lein-6b1bc0ca-676e-4487-810f-c017583b8a43/lib
Including lein-newnew-0.2.6.jar
Including stencil-0.2.0.jar
Including bultitude-0.1.5.jar
Including slingshot-0.8.0.jar
Created lein-newnew-0.2.6.jar


prayag@Prayag:~$ lein plugin install compojure/lein-template 0.2.0
[INFO] Unable to find resource 'compojure:lein-template:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'leinjacker:leinjacker:pom:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.pom from repository clojars at http://clojars.org/repo/
Transferring 3K from clojars
Downloading: trammel/trammel/0.7.0/trammel-0.7.0.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
[INFO] snapshot thneed:thneed:1.0.0-SNAPSHOT: checking for updates from clojars
[INFO] snapshot thneed:thneed:1.0.0-SNAPSHOT: checking for updates from central
Downloading: thneed/thneed/1.0.0-SNAPSHOT/thneed-1.0.0-20120330.013054-1.pom from repository clojars at http://clojars.org/repo/
Transferring 1K from clojars
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.jar from repository central at http://repo1.maven.org/maven2
Downloading: thneed/thneed/1.0.0-SNAPSHOT/thneed-1.0.0-20120330.013054-1.jar from repository clojars at http://clojars.org/repo/
Downloading: trammel/trammel/0.7.0/trammel-0.7.0.jar from repository clojars at http://clojars.org/repo/
Transferring 6K from clojars
Unable to locate resource in repository
[INFO] Unable to find resource 'leinjacker:leinjacker:jar:0.2.0' in repository central (http://repo1.maven.org/maven2)
Downloading: leinjacker/leinjacker/0.2.0/leinjacker-0.2.0.jar from repository clojars at http://clojars.org/repo/
Transferring 9K from clojars
Transferring 5K from clojars
Copying 3 files to /tmp/lein-77d9ecd7-c721-46ab-8760-5a5c9c2803df/lib
Including lein-template-0.2.0.jar
Including thneed-1.0.0-20120330.013054-1.jar
Including trammel-0.7.0.jar
Including leinjacker-0.2.0.jar
Created compojure-lein-template-0.2.0.jar


STEP 2 create a new project using the "compojure" template

prayag@Prayag:~$ sudo chmod 777 workspace_erujopmoc/
prayag@Prayag:~$ cd workspace_erujopmoc/
prayag@Prayag:~/workspace_erujopmoc$ lein new compojure hotel


STEP 3 Make sure project is created

prayag@Prayag:~/workspace_erujopmoc/hotel$ ls -la
total 44
drwxrwxr-x 7 prayag prayag 4096 Jan 14 18:09 .
drwxrwxrwx 3 root   root   4096 Jan 14 18:01 ..
drwxrwxr-x 2 prayag prayag 4096 Jan 14 18:09 classes
-rw-rw-r-- 1 prayag prayag  109 Jan 14 18:01 .gitignore
-rw-rw-r-- 1 prayag prayag   40 Jan 14 18:09 .lein-deps-sum
drwxrwxr-x 2 prayag prayag 4096 Jan 14 18:07 .lein-plugins
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:09 lib
-rw-rw-r-- 1 prayag prayag  309 Jan 14 18:01 project.clj
-rw-rw-r-- 1 prayag prayag  256 Jan 14 18:01 README.md
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:01 src
drwxrwxr-x 3 prayag prayag 4096 Jan 14 18:01 test


3.A) The dependencies are : 

(defproject hotel "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [compojure "1.1.1"]]
  :plugins [[lein-ring "0.7.1"]]
  :ring {:handler hotel.handler/app}
  :dev-dependencies [[ring-mock "0.1.2"]])


3.B) The structure of main is : 

prayag@Prayag:~/workspace_erujopmoc/hotel$ ls -l src/hotel/
total 4
-rw-rw-r-- 1 prayag prayag 252 Jan 14 18:01 handler.clj

The src/hotel/handler.clj reads as follws :

(ns hotel.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

Change (defroutes) to the following

(defroutes app-routes
  (GET "/" [] "Welcome To Rich Hickey Hotel")
  (route/not-found "Not Found"))


STEP 4 start a development server using Leiningen

prayag@Prayag:~/workspace_erujopmoc/hotel$ lein ring server
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.pom from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'lein-ring:lein-ring:pom:0.7.1' in repository central (http://repo1.maven.org/maven2)
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.pom from repository clojars at http://clojars.org/repo/
Transferring 2K from clojars
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.pom from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:data.xml:pom:0.0.3' in repository clojars (http://clojars.org/repo/)
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.pom from repository central at http://repo1.maven.org/maven2
Transferring 1K from central
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository sonatype-oss-snapshots at https://oss.sonatype.org/content/repositories/snapshots
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:pom.contrib:pom:0.0.25' in repository sonatype-oss-snapshots (https://oss.sonatype.org/content/repositories/snapshots)
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository clojars at http://clojars.org/repo/
Unable to locate resource in repository
[INFO] Unable to find resource 'org.clojure:pom.contrib:pom:0.0.25' in repository clojars (http://clojars.org/repo/)
Downloading: org/clojure/pom.contrib/0.0.25/pom.contrib-0.0.25.pom from repository central at http://repo1.maven.org/maven2
Transferring 5K from central
Downloading: org/clojure/data.xml/0.0.3/data.xml-0.0.3.jar from repository clojars at http://clojars.org/repo/
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.jar from repository central at http://repo1.maven.org/maven2
Unable to locate resource in repository
[INFO] Unable to find resource 'lein-ring:lein-ring:jar:0.7.1' in repository central (http://repo1.maven.org/maven2)
Downloading: lein-ring/lein-ring/0.7.1/lein-ring-0.7.1.jar from repository clojars at http://clojars.org/repo/
[...]
Copying 1 file to /home/prayag/workspace_erujopmoc/hotel/lib/dev
2013-01-14 18:09:24.142:INFO:oejs.Server:jetty-7.6.1.v20120215
Started server on port 3000
2013-01-14 18:09:24.218:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:3000


STEP 5 Hit http://localhost:3000/ and scream 

 Welcome To Rich Hickey Hotel



More On Compojure
[1] What's the “big idea” behind compojure routes?, http://stackoverflow.com/a/3490479/432903
https://docs.google.com/document/edit?id=1kKxwuPW0bkzxmbOvyMyJxfZNp71aMz4FifcVZclpTQY

[2] Is Clojure just "a Lisp syntax on the JVM"? Or is it more than that?, http://qr.ae/Ifo36

References
[1] Getting Started, https://github.com/weavejester/compojure/wiki/Getting-Started

[2] Installing Compojure from a clean Ubuntu, http://sharetheconversation.blogspot.com/2010/05/installing-compojure.html

[3] Using Leiningen to build Clojure code, http://alexott.net/en/clojure/ClojureLein.html#sec2

[4] Compojure Demystified with an example,
http://techbehindtech.com/2010/08/24/compojure-demystified-with-an-example-part-4/

No comments:

Post a Comment