Skip to content
Browse files

Simplify (routes), (wrap-routes)

Ensure parent middleware runs before child middleware.

Parent middleware should run before child middleware which means that it 
needs to be added last in the vector sequence, not first
  • Loading branch information...
swlkr committed Mar 19, 2019
1 parent bf1b91f commit 0c30a18c679ab6f63f93a8dde90af4597d781497
Showing with 31 additions and 30 deletions.
  1. +31 −30 src/coast/router.clj
@@ -204,9 +204,17 @@
[route middleware]
(let [[method uri val route-name] route
val (if (vector? val) val [val])
new-val (if (sequential? middleware)
(apply conj val middleware)
(conj val middleware))]
middleware (if (sequential? middleware)
[route mw] val
mw (if (or (sequential? mw)
(nil? mw))
new-val (-> (concat middleware mw)
(conj route)
(->> [method uri new-val route-name]
(filter some?)
@@ -226,25 +234,28 @@

(defn expand-resource [route]
(if (resource-route? route)
(->> (partition-by #(contains? #{:only :except} %) route)
(map resource-args)
(drop 1)
(apply resource))
(apply resource (drop 1 route))

(defn flatten-wrapped-routes [x]
(if (> (coast.utils/depth x) 1)
(mapcat flatten-wrapped-routes x)

(defn wrap-routes
"Wraps a given set of routes in the given functions"
[& args]
(let [routes (->> (filter sequential? args)
(partition-by verb?)
(partition 2)
(mapv #(vec (apply concat %)))
(mapcat expand-resource)
(let [fns (filter #(or (fn? %) (ident? %)) args)
routes (filter sequential? args)
routes (if (> (utils/depth routes) 2)
(first routes)
routes (flatten-wrapped-routes routes)
routes (->> (mapcat expand-resource routes)
(filter #(not (resource-route? %)))
fns (filter fn? args)]
(mapv #(wrap-route % fns) routes)))

(defn fallback-not-found [_]
@@ -457,19 +468,9 @@

(defn routes [& args]
(if (and (every? vector? args)
(every? true? (->> (mapcat identity args)
(map route?))))
(->> (apply concat args)
(mapcat expand-resource)
(filter #(not (resource-route? %)))
(->> (filter sequential? args)
(partition-by verb?)
(partition 2)
(mapv #(vec (apply concat %)))
(mapcat expand-resource)
(let [routes* (if (> (utils/depth args) 2)
(first args)
(->> (mapcat expand-resource routes*)
(filter #(not (resource-route? %)))
(mapv recreate-middleware-fns)

0 comments on commit 0c30a18

Please sign in to comment.
You can’t perform that action at this time.