Up and Running with Clojure

For the last three years or so, Clojure has been a language that I admired from afar: the design of the language is wonderful but I’ve never really used it to build anything and haven’t looked closely at the language in a while. Recently we had a Carbon Five tech showdown between Node.js and Ruby to see which system could pump out “Hello World” as fast and as consistently as possible. Since then we’ve added a Go version that impressed us a lot.

But we’re missing a JVM-based entry which gives me a great excuse to dive into the Clojure world and learn how things work.

Getting Started

Step 1 is to create a new project for our codebase. I installed Leiningen and ran:

lein new hellod

which creates our project structure along with placeholders for necessary files. We’ll add dependencies to our project.clj file as necessary, similar to a Gemfile in the Ruby world, including Clojure itself. Clojure is just a library for the Java VM so lein will download it like any other dependency. You should already have the JVM installed on your machine.

The Code

Now we need to implement our server in src/hellod/core.clj. Clojure doesn’t come with a simple HTTP server in its core libraries. We’ll use Aleph, which provides a simple HTTP server API on top of Java’s well-regarded Netty library.

In fact, Aleph has essentially the exact Hello World server we need to implement as an example in its README. Cut and paste for great victory!

We didn’t do a lot of coding in this blog post but we solved half of the problem with new environments: get something working. Now that we have a basic skeleton working, we can start learning new language features and libraries as we add functionality.

Carbon Five is a full service software consultancy that helps startups and established organizations design, build, and ship awesome products. If you have a project you’d like us to take a look at, or are interested in joining our team, please let us know.