Setup a Clojure Webserver with Jetty, Compojure, & Hiccup

Clojure is a versatile programming language founded on dynamic principles. Its speed and coherence have made it a desireable candidate for microservice achitectures, however the learning curve is steep.

Setting up a Clojure project

Clojures power also brings complexity. There are a number of ways to configure Clojure projects, but one of the most popular is leiningen. This simple configuration tool enables fast project setup, so you can focus on building.

Example Clojure webserver

Clojure is an ideal language for web projects, suitable for API and application development. This example project provides a webserver boilerplate with support for app routing and HTML templating.

Once Leiningen is installed follow these steps to setup a webserver at http://localhost:3000

git clone [email protected]:philipbeel/example-clojure-webserver-jetty-compojure-hiccup.git

cd example-clojure-webserver-jetty-compojure-hiccup

lein run

Webserver setup using Ring Jetty adapter

Ring Jetty adapter allows integration of a configurable webserver into any Clojure project, it provides options for threading, port configuration, and basic req/response structures. It can be used as the basis for web based projects.

Routing requests with Compojure

Compojure provides a comprehensive routing library. The example project implements a two routes, for an index page and a 404 page, demonstrating how these can be abstracted into a /routing directory.

Expressive HTML templating via Hiccup

Hiccup offers an expressive way to write HTML markup. The API reference documents how the structure can be extended to create dynamic markup. The example project demonstrates how this can be used in conjunction with script and stylesheets to render comprehensive HTML documents.

Further reading

Clojure is an extremly robust language, whilst setup can be challenging there are a great number of libraries that make it extremely enjoyable to work with. If you are interested in finding out more about Clojure, here are some recommended links:

Philip Beel

Read more posts by this author.