Created
January 2, 2014 05:30
-
-
Save dannypurcell/8215411 to your computer and use it in GitHub Desktop.
Ring middleware fn. Removes a trailing slash from the uri before calling the handler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn ignore-trailing-slash | |
"Modifies the request uri before calling the handler. | |
Removes a single trailing slash from the end of the uri if present. | |
Useful for handling optional trailing slashes until Compojure's route matching syntax supports regex. | |
Adapted from http://stackoverflow.com/questions/8380468/compojure-regex-for-matching-a-trailing-slash" | |
[handler] | |
(fn [request] | |
(let [uri (:uri request)] | |
(handler (assoc request :uri (if (and (not (= "/" uri)) | |
(.endsWith uri "/")) | |
(subs uri 0 (dec (count uri))) | |
uri)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment