Clack

Revenge of Lisp in Web

Chapter 9: Clack.Builder

English | 日本語

In this chapter, I tell you about Clack.Builder which is the most general way to use Clack Middleware. It builds up with calling wrap of Middlewares sequentially and returns a function also as an Application.

The syntax is easy to understand.

(builder
  ;; Write Middleware classes here.
  #'app)
;=> Function (as an Application)

And, here's a real sample code.

(import 'clack.builder:builder)

(defun app (env)
  (declare (ignore env))
  '(200
    (:content-type "text/plain")
    ("Hello, Clack!")))

(clack:clackup
  (builder
    <clack-middleware-session>
    (<clack-middleware-static>
      :path "/public/"
      :root #p"/path/to/static-files/")
    #'app))

<clack-middleware-session> is a middleware which enables a session management. By adding :clack.session to env, it allows to handle sessions as a hash table (See Clack.Middleware.Session).

Notice that builder calls wrap from bottom to top. In this example, #'app would be wrapped by <clack-middleware-static> first, and be wrapped by <clack-middleware-session> next.

Remember the figure in Chapter 7.

In case of this figure, the outer middleware is <clack-middleware-session> and the inner one is <clack-middleware-static>.