|
4 | 4 | [cheshire.core :as json] |
5 | 5 | [compojure.api.middleware :as mw] |
6 | 6 | [compojure.api.impl.logging :as logging] |
| 7 | + [compojure.api.common :as common] |
7 | 8 | [ring.swagger.common :as rsc] |
8 | 9 | [clojure.string :as str] |
9 | 10 | [linked.core :as linked] |
|
14 | 15 | ;; Route records |
15 | 16 | ;; |
16 | 17 |
|
17 | | -(def ^:dynamic *fail-on-missing-route-info* false) |
18 | | - |
19 | 18 | (defn- ->path [path] |
20 | 19 | (if-not (= path "/") path)) |
21 | 20 |
|
22 | 21 | (defn- ->paths [p1 p2] |
23 | 22 | (->path (str p1 (->path p2)))) |
24 | 23 |
|
25 | 24 | (defprotocol Routing |
26 | | - (get-routes [handler])) |
| 25 | + (-get-routes [handler options])) |
27 | 26 |
|
28 | 27 | (extend-protocol Routing |
29 | 28 | nil |
30 | | - (get-routes [_] [])) |
| 29 | + (-get-routes [_ _] [])) |
| 30 | + |
| 31 | +(defn filter-routes [{:keys [childs] :as handler} {:keys [invalid-routes-fn]}] |
| 32 | + (let [[valid-childs invalid-childs] (common/group-with (partial satisfies? Routing) childs)] |
| 33 | + (when (and invalid-routes-fn invalid-childs) |
| 34 | + (invalid-routes-fn handler invalid-childs)) |
| 35 | + valid-childs)) |
| 36 | + |
| 37 | +(defn get-routes |
| 38 | + ([handler] |
| 39 | + (get-routes handler nil)) |
| 40 | + ([handler options] |
| 41 | + (-get-routes handler options))) |
31 | 42 |
|
32 | 43 | (defrecord Route [path method info childs handler] |
33 | 44 | Routing |
34 | | - (get-routes [_] |
35 | | - (if (seq childs) |
36 | | - (vec |
37 | | - (for [[p m i] (mapcat get-routes (filter (partial satisfies? Routing) childs))] |
38 | | - [(->paths path p) m (rsc/deep-merge info i)])) |
39 | | - (into [] (if path [[path method info]])))) |
| 45 | + (-get-routes [this options] |
| 46 | + (let [valid-childs (filter-routes this options)] |
| 47 | + (if (seq childs) |
| 48 | + (vec |
| 49 | + (for [[p m i] (mapcat #(get-routes % options) valid-childs)] |
| 50 | + [(->paths path p) m (rsc/deep-merge info i)])) |
| 51 | + (into [] (if path [[path method info]]))))) |
40 | 52 |
|
41 | 53 | IFn |
42 | 54 | (invoke [_ request] |
|
45 | 57 | (AFn/applyToHelper this args))) |
46 | 58 |
|
47 | 59 | (defn create [path method info childs handler] |
48 | | - (when-let [invalid-childs (seq (remove (partial satisfies? Routing) childs))] |
49 | | - (let [message "Not all child routes satisfy compojure.api.routing/Routing." |
50 | | - data {:path path |
51 | | - :method method |
52 | | - :info info |
53 | | - :invalid invalid-childs}] |
54 | | - (if *fail-on-missing-route-info* |
55 | | - (throw (ex-info message data)) |
56 | | - (logging/log! :warn (str message ": " data))))) |
57 | 60 | (->Route path method info childs handler)) |
58 | 61 |
|
| 62 | +;; |
| 63 | +;; Invalid route handlers |
| 64 | +;; |
| 65 | + |
| 66 | +(defn fail-on-invalid-child-routes |
| 67 | + [handler invalid-childs] |
| 68 | + (throw (ex-info "Not all child routes satisfy compojure.api.routing/Routing." |
| 69 | + (merge (select-keys handler [:path :method]) {:invalid (vec invalid-childs)})))) |
| 70 | + |
| 71 | +(defn log-invalid-child-routes [handler invalid-childs] |
| 72 | + (logging/log! :warn (str "Not all child routes satisfy compojure.api.routing/Routing. " |
| 73 | + (select-keys handler [:path :method]) ", invalid child routes: " |
| 74 | + (vec invalid-childs)))) |
| 75 | + |
| 76 | + |
59 | 77 | ;; |
60 | 78 | ;; Swagger paths |
61 | 79 | ;; |
|
79 | 97 | (defn ring-swagger-paths [routes] |
80 | 98 | {:paths |
81 | 99 | (reduce |
82 | | - (fn [acc [path method info]] |
83 | | - (update-in |
84 | | - acc [path method] |
85 | | - (fn [old-info] |
86 | | - (let [info (or old-info info)] |
87 | | - (ensure-path-parameters path info))))) |
88 | | - (linked/map) |
89 | | - routes)}) |
| 100 | + (fn [acc [path method info]] |
| 101 | + (update-in |
| 102 | + acc [path method] |
| 103 | + (fn [old-info] |
| 104 | + (let [info (or old-info info)] |
| 105 | + (ensure-path-parameters path info))))) |
| 106 | + (linked/map) |
| 107 | + routes)}) |
90 | 108 |
|
91 | 109 | ;; |
92 | 110 | ;; Route lookup |
|
96 | 114 | (for [[id freq] (frequencies seq) |
97 | 115 | :when (> freq 1)] id)) |
98 | 116 |
|
99 | | -(defn route-lookup-table [handler] |
100 | | - (let [entries (for [[path endpoints] (-> handler get-routes ring-swagger-paths :paths) |
| 117 | +(defn route-lookup-table [routes] |
| 118 | + (let [entries (for [[path endpoints] (-> routes ring-swagger-paths :paths) |
101 | 119 | [method {:keys [x-name parameters]}] endpoints |
102 | 120 | :let [params (:path parameters)] |
103 | 121 | :when x-name] |
|
0 commit comments