Skip to content

Commit 68259c0

Browse files
committed
Add :module-middleware config option
1 parent 8c83af4 commit 68259c0

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,24 @@ handler.
3333

3434
There are four top level options available:
3535

36-
- `:routes` - the Reitit routing data
36+
- `:routes` - the Reitit routing data
3737
- `:middleware` - a vector of middleware to apply to the Ring handler
38-
- `:data` - data to add to every Reitit route
38+
- `:module-middleware` - the same as above, but designed for modules
39+
- `:data` - data to add to every Reitit route
3940
- `:handlers` - a vector of handlers to fall back
4041

4142
The `:data` key takes a map and acts as it does in Reitit, except for
4243
the following keys:
4344

4445
- `:muuntaja` - a map of Muuntaja options to be merged with the defaults
4546
- `:coercion` - one of: `:malli`, `:schema` or `:spec`
47+
- `:module-middleware` - a vector of middleware applied only if the
48+
route matches
4649

47-
These keys will automatically add relevant middleware.
50+
The `:module-middleware` keys work the same as the `:middleware` key but
51+
are applied last, as the outermost middleware. They are intended to allow
52+
a base set of middleware to be applied by modules, without interfering
53+
with middleware that is set by users.
4854

4955
The `:duct.handler.reitit/default` key will initiate into a handler
5056
using the Reitit `create-default-handler` function. It takes the

src/duct/router/reitit.clj

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@
4747
(-> (update :coercion (comp var-get requiring-resolve coercion-engines))
4848
(update :middleware #(into coercion-middleware %)))))
4949

50+
(defn- merge-module-middleware [options]
51+
(-> options
52+
(dissoc :module-middleware)
53+
(update :middleware #(into (vec (:module-middleware options)) %))))
54+
5055
(defmethod ig/init-key :duct.router/reitit [_ options]
5156
(let [opts (-> options
57+
(merge-module-middleware)
58+
(update-data merge-module-middleware)
5259
(update-data convert-coercion)
5360
(update-data convert-muuntaja))
5461
router (ring/router (:routes opts) opts)]
5562
(if-some [handlers (seq (:handlers opts))]
5663
(ring/ring-handler router (apply ring/routes handlers) opts)
57-
(ring/ring-handler router opts))))
64+
(ring/ring-handler router nil opts))))

test/duct/router/reitit_test.clj

+17
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@
6565
(router {:request-method :post, :uri "/"})))
6666
(is (= {:status 406, :body "406"}
6767
(router {:request-method :post, :uri "/406"})))))
68+
69+
(defn- wrap-header [handler header value]
70+
(fn [request]
71+
(-> (handler request)
72+
(assoc-in [:headers header] value))))
73+
74+
(deftest middleware-test
75+
(let [handler (constantly {:status 200, :body "Hello World"})
76+
config {:duct.router/reitit
77+
{:routes {"/" {:get {:handler handler}}}
78+
:module-middleware [[wrap-header "X-One" "1"]]
79+
:middleware [[wrap-header "X-Two" "2"]]
80+
:data {:module-middleware [[wrap-header "X-Three" "3"]]
81+
:middleware [[wrap-header "X-Four" "4"]]}}}
82+
router (:duct.router/reitit (ig/init config))]
83+
(is (= {"X-One" "1", "X-Two" "2", "X-Three" "3", "X-Four" "4"}
84+
(:headers (router {:request-method :get, :uri "/"}))))))

0 commit comments

Comments
 (0)