[hunchentoot-devel] Virtual hosts support using *META-DISPATCHER*

Ram Krishnan rkris at kriyative.net
Sun Jul 29 04:16:57 UTC 2007


Hi all,

I'm in the process of migrating a Web app from Portable Allegroserve
to Hunchentoot (0.10), and I had a question about implementing virtual
hosts by overriding the *META-DISPATCHER* function binding. Here's
what I have so far:

----------------

(defvar *vhost-dispatch-table-map* (make-hash-table :test 'string=))

(defun vhost-meta-dispatcher (server)
  (declare (ignore server))
  (or (gethash (hunchentoot:host) *vhost-dispatch-table-map*)
      *dispatch-table*))

(defun vhost-set-dispatch-table (hosts dispatch-table &key defaultp)
  (dolist (hostname (pg:mklist hosts))
    (setf (gethash hostname *vhost-dispatch-table-map*) dispatch-table))
  (when defaultp
    (setq *dispatch-table* dispatch-table)))

(setq hunchentoot:*meta-dispatcher* 'vhost-meta-dispatcher)

----------------

Now, I'm able to return different *DISPATCH-TABLE* bindings based on
the hostname, like so:

----------------

  (vhost-set-dispatch-table "foo.bar.com"
                            (list
                             (hunchentoot:create-folder-dispatcher-and-handler
                              "/assets/"
                              #p"/var/www/foo.bar.com/assets/")))
  (vhost-set-dispatch-table "quux.bar.com"
                            (list
                             (hunchentoot:create-folder-dispatcher-and-handler
                              "/assets/"
                              #p"/var/www/quux.bar.com/assets/")))

----------------

Are there any problems in subverting the *META-DISPATCHER* binding
this way? Is there a better alternative?

Thanks.

-ram



More information about the Tbnl-devel mailing list