Chapter 10. Clojure and the web

 

This chapter covers

  • Creating web services with Clojure
  • An overview of Ring
  • Using Compojure on top of Ring
  • Generating HTML

HTTP is the lingua franca of the internet. If your project involves creating a dynamic website, you’ll need to use a suitable web application framework to get your job done. Even if you aren’t creating a website for end users to visit, writing an HTTP interface to your application can provide an easy-to-use API for other applications. In this chapter, we’ll explore doing that. First, we’ll write some framework code, and then we’ll use some open source libraries to enhance what we wrote. Specifically, after writing our own little framework, we’ll explore the Ring and Compo-jure projects. We’ll also look at clj-html, another open source library that allows HTML generation via Clojure code.

We’ll write own little web framework to demonstrate how easy and straightforward Clojure is. This exercise will also serve as a segue to how you might use open source alternatives for web services in a production environment. Along the way, you’ll see some more idiomatic Clojure, which should help in other programs you might be writing. Let’s get started with our own take on an HTTP service in Clojure.

10.1. An HTTP interface from scratch

10.2. Ring

10.3. Compojure

10.4. Generating HTML

10.5. Summary