Ring Middleware in Depth
Learn how Ring middleware wraps handlers to add cross-cutting behavior like logging, parameter parsing, and session handling in Compojure web apps.
What Is Middleware?
In Ring, a handler is a function that takes a request map and returns a response map. Middleware is a higher-order function that takes a handler and returns a new handler.
This lets you wrap behavior around your core logic without modifying it.
(defn wrap-example [handler]
(fn [request]
(handler request)))The Wrapping Pattern
Every middleware follows the same shape: receive a handler, return a function that takes a request. You can act before or after calling the inner handler.
- Before: inspect or modify the request
- After: inspect or modify the response
(defn wrap-logging [handler]
(fn [request]
(println "Incoming:" (:uri request))
(let [response (handler request)]
(println "Status:" (:status response))
response)))All lessons in this course
- Introduction to Ring & HTTP Basics
- Routing with Compojure
- Handling Requests & Responses
- Ring Middleware in Depth