Context path in Compojure

459 views
Skip to first unread message

Shantanu Kumar

unread,
Jun 13, 2011, 8:27:18 AM6/13/11
to Clojure Web Development
Hi,

How can I access the context path when using Compojure? I need it for
generating URLs in the view template. It works fine when using with
"lein-ring" but when deployed in a WAR it needs to be prefixed with
the context path.

With lein-ring:
/path/to/static-file.png

In WAR:
/context/path/to/static-file.png

Regards,
Shantanu

David Jagoe

unread,
Jun 13, 2011, 8:43:14 AM6/13/11
to clojure...@googlegroups.com
This may help:


(ns clj-rheo.middleware.servlet-context
(:use [clj-rheo.util.core :only [get-context-path]]))

(defn wrap-context
"Removes the deployed servlet context from a URI when running as a
deployed web application"
[handler]
(fn [request]
(if-let [servlet-req (:servlet-request request)]
(let [context (get-context-path servlet-req)
uri (:uri request)]
(if (.startsWith uri context)
(handler (assoc request :uri
(.substring uri (.length context))))
(handler request)))
(handler request))))

James Reeves

unread,
Jun 13, 2011, 8:31:33 PM6/13/11
to clojure...@googlegroups.com
lein-ring adds :context and :path-info keys to the request map.

- James

Reply all
Reply to author
Forward
0 new messages