Skip to content

Instantly share code, notes, and snippets.

@lrenn
Created August 11, 2009 04:37
Show Gist options
  • Save lrenn/165634 to your computer and use it in GitHub Desktop.
Save lrenn/165634 to your computer and use it in GitHub Desktop.
(defn with-uri-rewrite
"Rewrites a request uri with the result of calling f with the
request's original uri. If f returns nil or the original uri no
rewrite occurs."
[handler f]
(fn [request]
(let [uri (:uri request)
rewrite (f uri)]
(if-not (or (nil? rewrite)
(= uri rewrite))
(handler (assoc request :uri rewrite))
(handler request)))))
(defn- remove-context
[uri context]
(if (.startsWith uri context)
(subs uri (count context))
uri))
(defn with-context
"Removes the context string from the beginning of the request uri
such that route matching occurs without it."
[handler context]
(with-uri-rewrite handler #(remove-context % context)))
(defn- uri-snip-slash
"Removes a trailing slash from all uris except \"/\"."
[uri]
(if (and (> (.length uri) 1)
(.endsWith uri "/"))
(chop uri)
uri))
(defn without-trailing-slash
[handler]
(with-uri-rewrite handler uri-snip-slash))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment