[hunchentoot-devel] Middleware with Hunchentoot

Mac Chan emailmac at gmail.com
Fri Jul 20 17:51:12 UTC 2007


On 7/20/07, Robert Uhl <eadmund42 at gmail.com> wrote:
> Then the user can force a page to use authentication with:
>
>   (create-regex-dispatcher "/foo/bar" (authentication #'my-handler))
>
> Instead of having to write any code inside MY-HANDLER.

If that's what you want (not having to write any code inside
handlers), and if all your handlers that require the same checking
share a common uri prefix (like everything under "/admin/..." will
require login, then you can probably just use Vagif's suggestion.

In my case I have no more than 20 define-easy-handlers so it was very
easy to manage by just adding a simple function call when needed (and
they don't need to share a common uri prefix).

> Because having to write:
>
> (defun display-foo-page (request)
>   (with-authentication
>     (with-caching
>       (with-etags
>         (with-gzip
>           (do-stuff))))))
>
> Gets unwieldy.
>
> But on reflection you're right: handler-specific code is probably best
> expressed as a WITH- macro.  Normally one would probably be doing
> something like:
>
> (defun display-foo-page (request)
>   (with-authorisation (maintainer editor)
>     (do-stuff)))

> (defun display-foo-page (request)
>   (with-authentication
>     (with-caching
>       (with-etags
>         (with-gzip
>           (do-stuff))))))

Exactly. The with-xxx macros are abstraction and you can also use it
indirectly. That's the beauty of macro. Once you have a set of these
simple building blocks you can easier group them together. Here's what
I'd typically do

(defmacro with-admin-template (&body body)
 `(with-authentication
   (with-caching
       (with-etags
           (with-gzip
               (with-html
                   (:html
                    (:head ...)
                    (:admin-header-html ...)
                    , at body
                    (:admin-footer-html ...))))))))


Notice there is a top level hunchentoot dispatch table, and then
define-easy-handler is _another_ level of dispatch table.

You can probably build another dispatch table for each uri prefix like
you suggested easily.
Have you looked at webaction's documentation? Is that what you want?

BTW, please excuse my pompous reply earlier. It was early in the
morning and I was feeling cranky.

Regards,
-- Mac



More information about the Tbnl-devel mailing list