File tree 3 files changed +34
-4
lines changed
3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -33,18 +33,24 @@ handler.
33
33
34
34
There are four top level options available:
35
35
36
- - ` :routes ` - the Reitit routing data
36
+ - ` :routes ` - the Reitit routing data
37
37
- ` :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
39
40
- ` :handlers ` - a vector of handlers to fall back
40
41
41
42
The ` :data ` key takes a map and acts as it does in Reitit, except for
42
43
the following keys:
43
44
44
45
- ` :muuntaja ` - a map of Muuntaja options to be merged with the defaults
45
46
- ` :coercion ` - one of: ` :malli ` , ` :schema ` or ` :spec `
47
+ - ` :module-middleware ` - a vector of middleware applied only if the
48
+ route matches
46
49
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.
48
54
49
55
The ` :duct.handler.reitit/default ` key will initiate into a handler
50
56
using the Reitit ` create-default-handler ` function. It takes the
Original file line number Diff line number Diff line change 47
47
(-> (update :coercion (comp var-get requiring-resolve coercion-engines))
48
48
(update :middleware #(into coercion-middleware %)))))
49
49
50
+ (defn- merge-module-middleware [options]
51
+ (-> options
52
+ (dissoc :module-middleware )
53
+ (update :middleware #(into (vec (:module-middleware options)) %))))
54
+
50
55
(defmethod ig /init-key :duct.router/reitit [_ options]
51
56
(let [opts (-> options
57
+ (merge-module-middleware )
58
+ (update-data merge-module-middleware)
52
59
(update-data convert-coercion)
53
60
(update-data convert-muuntaja))
54
61
router (ring/router (:routes opts) opts)]
55
62
(if-some [handlers (seq (:handlers opts))]
56
63
(ring/ring-handler router (apply ring/routes handlers) opts)
57
- (ring/ring-handler router opts))))
64
+ (ring/ring-handler router nil opts))))
Original file line number Diff line number Diff line change 65
65
(router {:request-method :post , :uri " /" })))
66
66
(is (= {:status 406 , :body " 406" }
67
67
(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 " /" }))))))
You can’t perform that action at this time.
0 commit comments