Compojure

Like most of James Reeves’ libraries, Compojure[22] inspires us to strive for elegance in our own work. If you’ve done any web work with Clojure, at some point you’ve probably used Compojure, on top of Ring,[23] to handle your HTTP routing. A typical app using Compojure starts out something like this:

apis/compojure_1.clj
 
(​ns​ hello-world
 
(:require [compojure.core :refer [defroutes GET]]
 
[compojure.route :refer [not-found]]))
 
 
(defroutes app
 
(GET ​"/"​ [] ​"Hello World"​)
 
(not-found ​"Page not found"​))

Before we look at the implementations of the Compojure bits that we’re using, take a moment to consider how concise this code is. Beyond the opening ns form, we need only a few lines of code (plus a bit of wiring in the

Get Mastering Clojure Macros now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.