Tuesday, August 11, 2009

Punting Static File Dispatch Back to web-server

Now - let's say you want to mix & match your URLs, so some are served with SHP, and others are handled by other servlets and file dispatcher of web-server.

There are multiple ways of achieving this of course - servlets can live in a separate directory, files can live in a separate directory, etc. It completely depends on how you set things up above the SHP handler.

Up to now, SHP handler is designed to work in single servlet setting, so it takes control over the whole dispatching process. In the future we can make it live under a particular prefix, but what we've got works for the single servlet purpose.

Now - how do you serve static files within SHP? We'll utilize and abstract over next-dispatcher so SHP will punt the static file dispatching back to web-server.

(require web-server/dispatchers/dispatch)

(define (punt!)

And what we want then is to have a particular shp page contain the following:


Then web-server will take over the handling of the static file dispatching (assuming that is the next dispatcher in web-server setting).

This comes in handy when you want to define a directory to hold, say, javascripts.

So you have a /scripts/* path in your web-server's htdocs path, and you want those to be served alongside of SHP, all you need is to add a /scripts/index.shp in your shp root path with the following:

;; /scripts/index.shp

With the combination of the pathinfo handling and the next-dispatching handling, your static javascripts now live side by side with your shp pages.

In order to make the shp page above work, we'll need to introduce it in the eval namespace as follows:

(define-namespace-anchor handler-namespace-anchor)

(define handler-namespace (namespace-anchor->namespace handler-namespace-anchor))

(define (make-shp-handler path
#:default (default "index.shp")
#:not-found (not-found "notfound.shp"))
(lambda (request)
(let ((terms (file->values (url->shp-path (request-uri request) path default not-found))))
(parameterize (($pathinfo ($pathinfo)))
(let ((proc (eval `(lambda (request) . ,terms) handler-namespace))) ;; pass the namespace to eval
(proc request))

Now you can serve any static files by defining the appropriate shp pages.

No comments:

Post a Comment