Codebase list comidi-clojure / 537b6db
Merge pull request #16 from scottyw/ring-handlers-only (maint) comidi routes can only map to Ring handlers Ruth Linehan 8 years ago
2 changed file(s) with 17 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
231231 it with the request as a parameter. (This code is largely copied from the
232232 bidi upstream, but we add support for inserting the match-context via
233233 middleware.)"
234 ([route handler-fn]
235 (fn [{:keys [uri path-info] :as req}]
236 (let [path (or path-info uri)
237 {:keys [handler route-params] :as match-context}
238 (or (:match-context req)
239 (apply bidi/match-route route path (apply concat (seq req))))]
240 (when handler
241 (bidi-ring/request
242 (handler-fn handler)
243 (-> req
244 (update-in [:params] merge route-params)
245 (update-in [:route-params] merge route-params))
246 (apply dissoc match-context :handler (keys req))
247 )))))
248 ([route] (make-handler route identity)))
234 [route]
235 (fn [{:keys [uri path-info] :as req}]
236 (let [path (or path-info uri)
237 {:keys [handler route-params] :as match-context}
238 (or (:match-context req)
239 (apply bidi/match-route route path (apply concat (seq req))))]
240 (when handler
241 (bidi-ring/request
242 handler
243 (-> req
244 (update-in [:params] merge route-params)
245 (update-in [:route-params] merge route-params))
246 (apply dissoc match-context :handler (keys req)))))))
249247
250248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
251249 ;;; Private - helpers for compojure-like syntax
347345
348346 (schema/defn ^:always-validate
349347 routes->handler :- (schema/pred fn?)
350 "Given a bidi route tree, converts into a ring request handler function. You
351 may pass an optional handler function which will be wrapped around the
352 bidi leaf."
353 ([routes :- bidi-schema/RoutePair
354 handler-fn :- (schema/maybe (schema/pred fn?))]
355 (let [compiled-routes (bidi/compile-route routes)]
356 (make-handler compiled-routes handler-fn)))
357 ([routes]
358 (routes->handler routes identity)))
348 "Given a bidi route tree, converts into a ring request handler function"
349 [routes :- bidi-schema/RoutePair]
350 (let [compiled-routes (bidi/compile-route routes)]
351 (make-handler compiled-routes)))
359352
360353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
361354 ;;; Public - compojure-like convenience macros
132132 (is (= {:foo "hi"
133133 :bar "there"}
134134 (handler {:uri "/foo/hi/bar/there"}))))))
135
136 (deftest routes->handler-fn-test
137 (let [handler (routes->handler
138 (context ["/foo/" :foo]
139 [["/bar/" :bar]
140 (fn [req] (:route-params req))])
141 (fn [f]
142 (fn [req]
143 {:result (into {} (map (fn [[k v]] [k (str "wrapped " v)])
144 (f req)))})))]
145 (is (= {:result {:foo "wrapped hi"
146 :bar "wrapped there"}}
147 (handler {:uri "/foo/hi/bar/there"})))))
148135
149136 (deftest compojure-macros-test
150137 (let [routes (context ["/foo/" :foo]