mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 08:21:11 +00:00
commit
c67a748915
6 changed files with 39 additions and 2 deletions
|
|
@ -147,3 +147,19 @@ Let's apply a small change to our ```ns3```. We'll replace our router by two dif
|
||||||
|
|
||||||
And there you have it, dynamic during dev, performance at production. We have it all !
|
And there you have it, dynamic during dev, performance at production. We have it all !
|
||||||
|
|
||||||
|
## Var handlers
|
||||||
|
|
||||||
|
You can use a var instead of a function as a `:handler`. This will
|
||||||
|
allow you to modify the handler function without rebuilding the reitit
|
||||||
|
router.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```clj
|
||||||
|
(def router
|
||||||
|
(ring/router
|
||||||
|
["/ping" {:get #'my-ns/handler}]))
|
||||||
|
```
|
||||||
|
|
||||||
|
Now you can reload `my-ns` or redefine `my-ns/handler` and the router
|
||||||
|
will use the new definition automatically.
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,10 @@
|
||||||
:cljs function)
|
:cljs function)
|
||||||
(expand [this _] {:handler this})
|
(expand [this _] {:handler this})
|
||||||
|
|
||||||
|
#?(:clj clojure.lang.Var
|
||||||
|
:cljs cljs.core.Var)
|
||||||
|
(expand [this _] {:handler this})
|
||||||
|
|
||||||
nil
|
nil
|
||||||
(expand [_ _]))
|
(expand [_ _]))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(s/def ::name keyword?)
|
(s/def ::name keyword?)
|
||||||
(s/def ::handler fn?)
|
(s/def ::handler (s/or :fn fn? :var var?))
|
||||||
(s/def ::no-doc boolean?)
|
(s/def ::no-doc boolean?)
|
||||||
(s/def ::conflicting boolean?)
|
(s/def ::conflicting boolean?)
|
||||||
(s/def ::default-data
|
(s/def ::default-data
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@
|
||||||
(:import (clojure.lang ExceptionInfo)
|
(:import (clojure.lang ExceptionInfo)
|
||||||
(reitit.core Router))))
|
(reitit.core Router))))
|
||||||
|
|
||||||
|
(defn- var-handler [& _]
|
||||||
|
"var-handler")
|
||||||
|
|
||||||
(deftest reitit-test
|
(deftest reitit-test
|
||||||
|
|
||||||
(testing "routers handling wildcard paths"
|
(testing "routers handling wildcard paths"
|
||||||
|
|
@ -245,7 +248,11 @@
|
||||||
(let [router (r/router ["/ping" (constantly "ok")])
|
(let [router (r/router ["/ping" (constantly "ok")])
|
||||||
{:keys [result]} (r/match-by-path router "/ping")]
|
{:keys [result]} (r/match-by-path router "/ping")]
|
||||||
(is result)
|
(is result)
|
||||||
(is (= "ok" (result))))))
|
(is (= "ok" (result))))
|
||||||
|
(testing "var handler gets expanded"
|
||||||
|
(let [router (r/router ["/ping" #'var-handler])
|
||||||
|
{:keys [result]} (r/match-by-path router "/ping")]
|
||||||
|
(is (= #'var-handler result))))))
|
||||||
|
|
||||||
(testing "custom router"
|
(testing "custom router"
|
||||||
(let [router (r/router ["/ping"] {:router (fn [_ _]
|
(let [router (r/router ["/ping"] {:router (fn [_ _]
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@
|
||||||
["/api" {:middleware [api-mw]}
|
["/api" {:middleware [api-mw]}
|
||||||
["/all" handler]
|
["/all" handler]
|
||||||
["/get" {:get handler}]
|
["/get" {:get handler}]
|
||||||
|
["/get-var" {:get {:handler #'handler}}]
|
||||||
["/users" {:middleware [[mw :users]]
|
["/users" {:middleware [[mw :users]]
|
||||||
:get handler
|
:get handler
|
||||||
:post {:handler handler
|
:post {:handler handler
|
||||||
|
|
@ -74,6 +75,10 @@
|
||||||
(app {:uri "/api/get" :request-method :get})))
|
(app {:uri "/api/get" :request-method :get})))
|
||||||
(is (= nil (app {:uri "/api/get" :request-method :post}))))
|
(is (= nil (app {:uri "/api/get" :request-method :post}))))
|
||||||
|
|
||||||
|
(testing "var handler"
|
||||||
|
(is (= {:status 200, :body [:api :ok]}
|
||||||
|
(app {:uri "/api/get-var" :request-method :get}))))
|
||||||
|
|
||||||
(testing "expanded method handler"
|
(testing "expanded method handler"
|
||||||
(is (= {:status 200, :body [:api :users :ok]}
|
(is (= {:status 200, :body [:api :users :ok]}
|
||||||
(app {:uri "/api/users" :request-method :get}))))
|
(app {:uri "/api/users" :request-method :get}))))
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,11 @@
|
||||||
["/api" {:name "kikka"}]
|
["/api" {:name "kikka"}]
|
||||||
{:validate rs/validate}))))
|
{:validate rs/validate}))))
|
||||||
|
|
||||||
|
(testing "handler can be a var"
|
||||||
|
(is (r/router? (r/router
|
||||||
|
["/api" {:handler #'identity}]
|
||||||
|
{:validate rs/validate}))))
|
||||||
|
|
||||||
(testing "spec can be overridden"
|
(testing "spec can be overridden"
|
||||||
(is (r/router? (r/router
|
(is (r/router? (r/router
|
||||||
["/api" {:handler "identity"}]
|
["/api" {:handler "identity"}]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue