-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathmiddleware.clj
More file actions
41 lines (37 loc) · 1.55 KB
/
Copy pathmiddleware.clj
File metadata and controls
41 lines (37 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(ns compojure.middleware
"Optional middleware to enhance routing in Compojure."
(:require [compojure.core :refer [wrap-routes]]
[ring.util.response :as resp]))
(defn remove-trailing-slash
"Remove the trailing '/' from a URI string, if it exists."
[^String uri]
(if (.endsWith uri "/")
(.substring uri 0 (dec (.length uri)))
uri))
(defn- redirect-to-canonical
([request]
(resp/redirect (:compojure/path request) 301))
([request respond raise]
(respond (redirect-to-canonical request))))
(defn- assoc-path [request path]
(assoc request :compojure/path path))
(defn wrap-canonical-redirect
"Middleware that permanently redirects any non-canonical route to its
canonical equivalent, based on a make-canonical function that changes a URI
string into its canonical form. If not supplied, the make-canonical function
will default to [[remove-trailing-slash]]."
([handler]
(wrap-canonical-redirect handler remove-trailing-slash))
([handler make-canonical]
(let [redirect-handler (wrap-routes handler (constantly redirect-to-canonical))]
(fn
([{uri :uri :as request}]
(let [canonical-uri (make-canonical uri)]
(if (= uri canonical-uri)
(handler request)
(redirect-handler (assoc-path request canonical-uri)))))
([{uri :uri :as request} respond raise]
(let [canonical-uri (make-canonical uri)]
(if (= uri canonical-uri)
(handler request respond raise)
(redirect-handler (assoc-path request canonical-uri) respond raise))))))))