Clack contains these useful middlewares:
- Clack.Middleware.Static - returns static files
- Clack.Middleware.Logger - logs messages
- Clack.Middleware.Session - manages sessions
- Clack.Middleware.Conditional - enables a middleware to conditionally wraps components
- Clack.Middleware.Auth.Basic - provides basic authentication
Clack.Middleware.Csrf which protects Clack applications from CSRF attacks is also available as a contrib package.
Let’s take a look at Clack.Middleware.Static as a real example of a Clack middleware. It returns a static file if a request matches a specified pattern.
Here’s an example source code to show how to use Clack.Middleware.Static. This
wrapped application returns “Hello, Clack!” for most requests, but a file in
/path/to/static-files/ when request pathes match
(import '(clack:wrap clack.middleware.static:<clack-middleware-static>)) (defun app (env) (declare (ignore env)) '(200 (:content-type "text/plain") ("Hello, Clack!"))) (defvar mw (make-instance '<clack-middleware-static> :path "/public/" :root #p"/path/to/static-files/")) (clack:clackup (wrap mw #'app))
Every middleware implements a
wrap method, which provides a mechanism to
generate wrapped applications. An application can be wrapped by multiple
middlewares through calling
(clack:clackup (wrap mw3 (wrap mw2 (wrap mw1 #'app))))
However, this nesting
wraps looks tedious. Clack has a solution to write this
with a DSL style syntax. That’s the topic of the next chapter.