mirror of
https://github.com/metosin/reitit.git
synced 2025-12-30 05:08:25 +00:00
Add 0-ary version
This commit is contained in:
parent
ad8384022c
commit
b6e8a9d579
1 changed files with 20 additions and 19 deletions
|
|
@ -120,25 +120,26 @@
|
|||
| | :strip - redirects slashed to slash-less |
|
||||
| | :both - works both ways (default) |
|
||||
"
|
||||
[{:keys [method] :or {method :both}}]
|
||||
(let [redirect-handler (fn redirect-handler [request]
|
||||
(let [uri (:uri request)
|
||||
status (if (= (:request-method request) :get) 301 308)
|
||||
maybe-redirect (fn maybe-redirect [path]
|
||||
(if (r/match-by-path (::r/router request) path)
|
||||
{:status status
|
||||
:headers {"Location" path}
|
||||
:body ""}))]
|
||||
(if (str/ends-with? uri "/")
|
||||
(if (not= method :add)
|
||||
(maybe-redirect (subs uri 0 (-> uri count dec))))
|
||||
(if (not= method :strip)
|
||||
(maybe-redirect (str uri "/"))))))]
|
||||
(fn
|
||||
([request]
|
||||
(redirect-handler request))
|
||||
([request respond _]
|
||||
(respond (redirect-handler request))))))
|
||||
([] (redirect-trailing-slash-handler {:method :both}))
|
||||
([{:keys [method]}]
|
||||
(let [redirect-handler (fn redirect-handler [request]
|
||||
(let [uri (:uri request)
|
||||
status (if (= (:request-method request) :get) 301 308)
|
||||
maybe-redirect (fn maybe-redirect [path]
|
||||
(if (r/match-by-path (::r/router request) path)
|
||||
{:status status
|
||||
:headers {"Location" path}
|
||||
:body ""}))]
|
||||
(if (str/ends-with? uri "/")
|
||||
(if (not= method :add)
|
||||
(maybe-redirect (subs uri 0 (-> uri count dec))))
|
||||
(if (not= method :strip)
|
||||
(maybe-redirect (str uri "/"))))))]
|
||||
(fn
|
||||
([request]
|
||||
(redirect-handler request))
|
||||
([request respond _]
|
||||
(respond (redirect-handler request)))))))
|
||||
|
||||
(defn create-default-handler
|
||||
"A default ring handler that can handle the following cases,
|
||||
|
|
|
|||
Loading…
Reference in a new issue