Chapter 3Web Services

Clojure is a great general-purpose language, but it really shines when it comes to web services. Clojure's characteristic focus on immutable data structures, safe concurrency, and code re-use allows you to write services that are composable, reliable, and expressive.

In this chapter, we'll introduce the fundamental abstractions common to virtually all Clojure services: HTTP handlers, middleware, and routing. You'll use the popular Compojure template, which is based on Ring for HTTP abstraction and Compojure for routing. After a tour of the building blocks of a web service, you'll actually write one from start to finish: a storage-backed link shortener. The service will let users submit a full URL and a short name, then redirect requests to that short name back to the original URL. Finally, we'll briefly cover some of the popular options for deploying Clojure web services in development or production, either locally or to the cloud.

PROJECT OVERVIEW

For this example project, you're going to use the standard compojure template:

lein new compojure example-project

The result is something very similar to the default template, but with a few extras:

  1. An added dependency for Compojure, a popular request routing library
  2. A development-only dependency on ring-mock for testing
  3. The lein-ring ...

Get Professional Clojure 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.