April 18th, 2010

How to setup a simple Compojure project

Written by Dave BarkerTopics: Compojure, Clojure, Howto

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

An idea for a better webmail

Written by Dave BarkerTopics: Thoughts, Idea

Currently webmail is broken, Squirrelmail looks old and dated, Roundcube got me hacked and Gmail isn't opensource and on my server.

So here's how to fix it, write a restful JSON API to perform all the functions like connecting, listing messages, sending emails etc. that works in the same way as CouchDB.

Next make a PHP theme to use this new API to create a Squirrelmail clone, another one for use from mobile devices and a third snazzy theme using just HTML and Javascript for desktop use.

The API could be written in anything but I suggest PHP just for practical deployment everywhere that Squirrelmail worked.

I would call it Davemail and lay it out something like this:

davemail/api/* - for all the actual requests to access imap

davemail/themes/* - for the different themes, one per directory that can be easily indexed dynamically so people can just chuck new themes inside and use them without any configuration

davemail/config/ - Everything inside the config directory would get read by davemail for all the configuration details. That way it could just be a symlink to /etc/davemail/ or wherever. The files would set which imap server to use, login details, email domains accepted etc

Once Davemail takes off the API can be re-written in whatever language or with whatever optimization tricks that we might want in the future and all the themes will still work.

The first step is writing the API, I think we should start with the Squirrelmail IMAP includes and modify them to return the JSON expected.

April 15th, 2010

An idea for a better keyboard

Written by Dave BarkerTopics: Idea, Thoughts

The need for ergonomic keyboards is clear, and so far the only one is the Datahand.

It's clear that a keyboard should be shaped to your hand if it has any chance of being ergonomic. In fact it should fit you like a glove.

Well that's my idea, take some of those switches and wires that everyone currently uses to make stupid light-up t-shirts and apply them to something worthwhile.

Take a stretchy "one size fits all" glove, add switches to each finger tip and a usb cable and you could have a product BETTER than the Datahand for a fraction of the price.

Please someone take this idea and get rich, just give me a free pair of your new keyboard gloves.

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.

April 09th, 2010

Using the word "orthogonal" does not make you as smart as Rich Hickey

Written by Dave BarkerTopics: Clojure, Thoughts

This is kind of a stupid post but here we go. Please can everyone tone down use of the word "orthogonal" a bit? it doesn't make you smarter and in fact if you don't even look the word up first you can end up looking pretty dumb. (Hell I didn't know what it meant and I had to pause the video and look it up.)

I noticed how everyone emulated Rich after watching through the Clojure videos and you could barely read anything about Clojure without every third word being orthogonal. The final straw was a interview I listened to where the host didn't know the difference between recursion and orthogonal and kept mixing them up.

Older posts