Posts tagged Compojure

April 18th, 2010

How to setup a simple Compojure project

Written by Dave BarkerTopics: Compojure, Clojure, Howto, Code

If you read my last Compojure post you will soon discover (like me) that you need to put in a little bit more work to get a practical setup. Thanks goes to arbscht in #compojure for showing me all of this.

So first up you need to create a new Leiningen project. Luckily there's an in-built command to help.

lein new example
cd example

Now you should be able to see the skeleton of your new project. Next you need to setup the project.clj file, it's used to set the name and dependencies for the project.

You will need to add a description, adjust the version and add the required dependencies for a Compojure based website. Here's what an example:

(defproject example "0.0.1"
  :description "Test website to learn Compojure"
  :dependencies [[org.clojure/clojure "1.1.0"]
		 [org.clojure/clojure-contrib "1.1.0"]
		 [compojure "0.4.0-SNAPSHOT"]
		 [hiccup "0.2.3"]
		 [ring/ring-jetty-adapter "0.2.0"]
		 [leiningen/lein-swank "1.2.0-SNAPSHOT"]])

Now inside ./src/example/ we need to create a server.clj we can use to setup the webserver, here's mine:

(ns example.server
  (:use compojure.core
	ring.adapter.jetty))

(defroutes main-routes
  (GET "/" []
    "<h1>Hello World</h1>")
  (GET "/test/:name" req
       (test-page (:name (:params req))))
  (ANY "*" []
    {:status 404, :body "<h1>Page not found</h1>"}))

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

(defn test-page [name]
  (str "<b>Hello" name "</b>"))

Three interesting things to note with that example:

  1. The call to run-jetty has been put inside a function. That way when the file is included the server doesn't automatically start. Inconvenient but it lets us :reload the file as much as we need.
  2. run-jetty is passed #'main-routes instead of just main-routes, that is useful so that any changes to our routes are put in effect as soon as the (defroutes main-routes ...) s-exp is re-evaled.
  3. The page /test/* is going to display "Hello *" as an example of how to capture "parameters" from the user. Try just passing req instead of (:name (:params req)) to see what else is available to you.

Now we are ready to give this a go, make sure you're in the root of your project and type

lein deps
lein swank

To start a Clojure process for your project that also runs a Swank server. Then you just need to open Emacs and do the following:

M-x slime-connect ret ret
(use 'example.server)
(example.server/start)

Now browse to http://localhost:8080 and you should see a hello world page. Browse to http://localhost:8080/test/Dave and it should say "Hello Dave". Finally change your server.clj to say "Hello new world" and re-eval the (defroutes ...). The change should take effect as soon as you refresh your browser!

Cheers, Dave.

April 15th, 2010

Installing Compojure

Written by Dave BarkerTopics: Compojure, Clojure, Howto

The Clojure webframework Compojure is dead easy to install and get running but I got stuck. Anyway it's working now and here are the steps I took:

Install Leiningen (as root)

cd /usr/local/bin/
wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
chmod a+x lein

Setup Leningen and install Compojure (as user)

lein self-install

git clone git://github.com/weavejester/compojure.git
cd compojure
lein deps
lein jar
cp compojure.jar ~/.swank-clojure/
cp libs/* ~/.swank-clojure/

(~/.swank-clojure/ is inside my classpath because I use Emacs, replace that with the correct directory for your installation.)

Now a trial program straight from the Compojure homepage

(use 'compojure.core 'ring.adapter.jetty)

(defroutes main-routes
  (GET "/" []
    "<h1>Hello World</h1>")
  (ANY "*" []
    {:status 404, :body "<h1>Page not found</h1>"}))

(run-jetty main-routes {:port 8080})

Now browse to http://localhost:8080 and it should say "Hello World".

Cheers, Dave.