Skip to content

Commit

Permalink
Merge pull request #585 from djblue/var-handler
Browse files Browse the repository at this point in the history
Allow var handlers
  • Loading branch information
opqdonut authored Mar 15, 2024
2 parents 2fe448c + d24b501 commit c67a748
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
16 changes: 16 additions & 0 deletions doc/advanced/dev_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 !

## 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.
4 changes: 4 additions & 0 deletions modules/reitit-core/src/reitit/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
:cljs function)
(expand [this _] {:handler this})

#?(:clj clojure.lang.Var
:cljs cljs.core.Var)
(expand [this _] {:handler this})

nil
(expand [_ _]))

Expand Down
2 changes: 1 addition & 1 deletion modules/reitit-core/src/reitit/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
;;

(s/def ::name keyword?)
(s/def ::handler fn?)
(s/def ::handler (s/or :fn fn? :var var?))
(s/def ::no-doc boolean?)
(s/def ::conflicting boolean?)
(s/def ::default-data
Expand Down
9 changes: 8 additions & 1 deletion test/cljc/reitit/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
(:import (clojure.lang ExceptionInfo)
(reitit.core Router))))

(defn- var-handler [& _]
"var-handler")

(deftest reitit-test

(testing "routers handling wildcard paths"
Expand Down Expand Up @@ -245,7 +248,11 @@
(let [router (r/router ["/ping" (constantly "ok")])
{:keys [result]} (r/match-by-path router "/ping")]
(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"
(let [router (r/router ["/ping"] {:router (fn [_ _]
Expand Down
5 changes: 5 additions & 0 deletions test/cljc/reitit/ring_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
["/api" {:middleware [api-mw]}
["/all" handler]
["/get" {:get handler}]
["/get-var" {:get {:handler #'handler}}]
["/users" {:middleware [[mw :users]]
:get handler
:post {:handler handler
Expand All @@ -74,6 +75,10 @@
(app {:uri "/api/get" :request-method :get})))
(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"
(is (= {:status 200, :body [:api :users :ok]}
(app {:uri "/api/users" :request-method :get}))))
Expand Down
5 changes: 5 additions & 0 deletions test/cljc/reitit/spec_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
["/api" {:name "kikka"}]
{: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"
(is (r/router? (r/router
["/api" {:handler "identity"}]
Expand Down

0 comments on commit c67a748

Please sign in to comment.