Currently, if you're going to wrap a handler in the session
middleware, you need to ensure it does not return nil. I guess I'll
add something in to fix this in Ring, but in general, Ring middleware
expects a valid response map, whilst Compojure will return nil if no
route matches.
Because you haven't added a catch-all 404 route, you'll get that
NullPointerException every time someone accesses a resource other than
"/template/:id/". For instance, perhaps the browser is attempting to
retrieve the favicon.
- James
Yes, but in your example code, you don't have a catch-all route.
> but this page also tries to load some css and images, which are served
> by ring file handler (i've written post about problem in it)
I don't see this in your example?
Also, you probably want the compojure.route/files function for serving
static files.
Here's what I think your routes should look like:
(require '[compojure.route :as route])
(use '[clojure.contrib.duck-steams :only file])
(use '[ring.middleware.session :only wrap-session])
(defroutes template-routes
(GET "/template/:id" [id]
(tmp-controller id))
(wrap! template-routes :mw/layout)
(defroutes all-routes
template-routes
(route/files "/")
(route/not-found (file "public/404.html")))
(wrap! all-routes :session)
The route/files function will load files from ./public by default, but
you can override this by adding an option map with {:root
"./your-static-files"}.
We're adding your custom layout middleware to your template-routes,
but the session to all-routes. This ensures the session is global.
Keywords can be used to indicate "wrap-" for the wrap! macro. So
(wrap! x :y) is the same as (wrap! x wrap-y).
- James
wrap-file-info guesses the mime-type from the file extension, and then
adds that to the Content-Type header. That tells the browser what type
of file is being sent.
I'm guessing it doesn't work for you because SimpleDateFormat takes
into current locale, whilst the HTTP date format always uses English
month and day names. I suspect we need to patch Ring to force the date
format to use an English locale.
I'll try to do something about it this weekend, when I'm over my
jetlag. If you can solve this and submit a patch in the meantime,
it'll speed up the process :)
- James