Professional Clojure
by Jeremy Anderson, Michael Gaare, Justin Holguín, Nick Bailey, Timothy Pratley
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:
- An added dependency for Compojure, a popular request routing library
- A development-only dependency on ring-mock for testing
- The lein-ring ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access