Extend Ring
Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (16 / 107)
👈 Route Requests with Ring | TOC | Define the Routes with Rei tit 👉
Ring provides a simple base for handling the HTTP request and response cycle. We’ll now look at several libraries that provide many additional utility functions that extend the functionality of Ring.
As we saw earlier, the Ring stack consists of a chain of middleware functions. Each function accepts the request map, modifies it in some way, and then passes it on to the next function in the chain. The middleware functions have to be wrapped in order of dependency. For example, session-based authentication relies on the presence of a session, and the session middleware must run before it to make the session available.
A library called ring-defaults provides a standard set of middleware that’s useful for typical web applications. The middleware is split into API middleware and site middleware. The configuration api-defaults is meant to be used for web service APIs, and its counterpart, secure-api-defaults, extends it with SSL redirects and enables HSTS. Conversely, site-defaults provide the middleware stack for a typical website, while secure-site-defaults provide security extensions such as SSL and secure cookies. Luminus defaults to using the site-defaults middleware.
Ideally, the responses returned by the server should follow the HTTP status codes whenever appropriate. When…