On Nov 4, 1:05 am, Justin Henzie <
jhen...@mac.com> wrote:
> My assumption is that the APP_DIR represents root for the application
> context and thus either of these paths should resolve to the named
> image.
Compojure does not serve static files by default, as a developer may
not actually want any static files (such as in the case of a RESTful
web API). Similarly, it doesn't automatically deal out 404s, either.
Compojure doesn't make any assumptions about what you want to do; you
have to be explicit.
So in order to serve static files, you typically need to add in two
handlers. One to serve files, and the other to raise a 404 error if
the first handler fails:
(defservlet file-example
(GET "/public/*"
(or (serve-file (route :*)) :next))
(ANY "*"
(page-not-found)))
This example uses two functions: serve-file and page-not-found. Here
are their respective docstrings:
(page-not-found)
(page-not-found filename)
A shortcut to create a '404 Not Found' HTTP response.
(serve-file path)
(serve-file root path)
Attempts to serve up a static file from a directory, which defaults to
'./public'. Nil is returned if the file does not exist. If the file is
a
directory, the function looks for a file in the directory called
'index.*'.
So we try to serve a file, and if that comes up as nil, then we return
the keyword :next. This tells Compojure to keep looking for a matching
handler.
You don't have to use "/public/*" as the route to your static files,
either. You can just use "/*":
(defservlet file-example
; Put custom code here
(GET "/*"
(or (serve-file (route :*)) :next))
(ANY "*"
(page-not-found)))
Remember to ensure that the (ANY "*" ...) route is always at the
bottom. Handlers are matched in order, so you'll want your "catch-all"
404 handler to be matched last, when everything else fails.
Does that help answer your question?
- James