[kpax-devel] static path

Sven Van Caekenberghe scaekenberghe at common-lisp.net
Thu May 24 11:46:39 UTC 2007


Matthias,

On 20 May 2007, at 09:47, Matthias Teege wrote:

> I'm not sure where to put static pages in a kpax app. If I write data
> from my app they are relative to the home of the application but if I
> set '(:static-root "static/")' and put files in apphome/static I've  
> got
>  S-HTTP-SERVER: [4] No handler found for #<S-HTTP-SERVER:HTTP- 
> REQUEST GET "/kpax/static/wapp/wapp.css" {A93DB89}>
>
> How do I access the static files?

Let's recap a little bit.

KPAX webapps work with 2 kinds of computed URL's: dynamic URLs and  
static URLs. Apart from those, you can of course insert any URL in  
your HTML. The reason to separate the static ones from the dynamic  
ones is to have a later option to optimize: in a very heavy loaded  
production web app, Lisp should only serve the dynamic content, while  
apache or a content delivery network should serve all the static  
content.

The function DYNAMIC-URL generates a new URL in the context of the  
current webapp, given a number of parameter, typically a handler- 
function name and a couple of variables name-and-value pairs.

The function STATIC-URL generates a new URL in the context or scope  
of the current webapp or the current web app server, most often with  
a static resource name, but format-based substitution is also possible.

If the scope is :SERVER, the static path of the server is used as  
prefix.

KPAX-USER 5 > (get-static-prefix *web-app-server*)
"/kpax/static/"

If the scope is :WEBAPP, the static path of the server is combined  
(concatenated) with that of the web app itself (normally its name)  
and used as a prefix.

KPAX-USER 11 > (kpax::get-complete-static-prefix :webapp (get-web- 
app :helloworld1) *web-app-server*)
"/kpax/static/helloworld1/"

So, using this in a webapp's handler we get:

KPAX-USER 13 > (static-url *last-request-response* :server "nx.css")
"/kpax/static/nx.css"

KPAX-USER 14 > (static-url *last-request-response* :webapp "kpax- 
movie-poster.jpg")
"/kpax/static/helloworld1/kpax-movie-poster.jpg"

KPAX-USER 15 > (dynamic-url *last-request-response* 'helloworld1-start)
"/kpax/dynamic/helloworld1/helloworld1-start"

Now, the URL is only one part: the resource in specified the URL has  
yet to be served by some code. In the case of dynamic-url's this is  
of course always Lisp. In the case of static-url's there are some  
options (which also depend on how KPAX is used, s-http-server, mod- 
lisp or portable allegro serve). In the mod-lisp case, everything  
must always be done explicitely by hand.

To make development easy, there is an option called :static-root for  
web-apps. If that option is specified, KPAX will try to serve static  
files from a directory named in the option, relative to the location  
of the webapp's source file.

For the HELLOWORLD1 example, the definition is:

(defwebapp :helloworld1
   (:index 'helloworld1-start)
   (:static-root "static/")
   (:unsecure t))

Using the S-HTTP-SERVER option, a static resource server routing will  
be set up that translates the prefix "/kpax/static" to (in my case,  
helloworld1.lisp is located in "/Users/sven/darcs/kpax/example/") the  
actual directory "/Users/sven/darcs/kpax/example/static".
The 2 static examples above then become:

"/kpax/static/nx.css" -> /Users/sven/darcs/kpax/example/static/nx.css"

"/kpax/static/helloworld1/kpax-movie-poster.jpg" -> /Users/sven/darcs/ 
kpax/example/static/helloworld1/kpax-movie-poster.jpg"

You can inspect some of the internals of S-HTTP-SERVER at the URL /s- 
http-server where you can see the actual routing info.

As I said before, in a production case, you wouldn't use or at least  
override this automatic mechanism and manually configure the serving  
of static resources.

In your example, the actual file path should be <apphome>/static/wapp/ 
wapp.css


Regards,

Sven







More information about the kpax-devel mailing list