Codebase list comidi-clojure / 3121f4a
(maint) comidi routes can only map to Ring handlers Bidi allows routes to map to any value, not just Ring handlers. Comidi expects the value to be a Ring handler (and only ever provides one itself) but exposes a couple of overloaded functions that allow a user to in principle provide something different. This commit removes that ability. Scott Walker 8 years ago
2 changed file(s) with 17 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
230230 it with the request as a parameter. (This code is largely copied from the
231231 bidi upstream, but we add support for inserting the match-context via
232232 middleware.)"
233 ([route handler-fn]
234 (fn [{:keys [uri path-info] :as req}]
235 (let [path (or path-info uri)
236 {:keys [handler route-params] :as match-context}
237 (or (:match-context req)
238 (apply bidi/match-route route path (apply concat (seq req))))]
239 (when handler
240 (bidi-ring/request
241 (handler-fn handler)
242 (-> req
243 (update-in [:params] merge route-params)
244 (update-in [:route-params] merge route-params))
245 (apply dissoc match-context :handler (keys req))
246 )))))
247 ([route] (make-handler route identity)))
233 [route]
234 (fn [{:keys [uri path-info] :as req}]
235 (let [path (or path-info uri)
236 {:keys [handler route-params] :as match-context}
237 (or (:match-context req)
238 (apply bidi/match-route route path (apply concat (seq req))))]
239 (when handler
240 (bidi-ring/request
241 handler
242 (-> req
243 (update-in [:params] merge route-params)
244 (update-in [:route-params] merge route-params))
245 (apply dissoc match-context :handler (keys req)))))))
248246
249247 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250248 ;;; Private - helpers for compojure-like syntax
315313
316314 (schema/defn ^:always-validate
317315 routes->handler :- (schema/pred fn?)
318 "Given a bidi route tree, converts into a ring request handler function. You
319 may pass an optional handler function which will be wrapped around the
320 bidi leaf."
321 ([routes :- bidi-schema/RoutePair
322 handler-fn :- (schema/maybe (schema/pred fn?))]
323 (let [compiled-routes (bidi/compile-route routes)]
324 (make-handler compiled-routes handler-fn)))
325 ([routes]
326 (routes->handler routes identity)))
316 "Given a bidi route tree, converts into a ring request handler function"
317 [routes :- bidi-schema/RoutePair]
318 (let [compiled-routes (bidi/compile-route routes)]
319 (make-handler compiled-routes)))
327320
328321 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
329322 ;;; Public - compojure-like convenience macros
133133 :bar "there"}
134134 (handler {:uri "/foo/hi/bar/there"}))))))
135135
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"})))))
148
149136 (deftest compojure-macros-test
150137 (let [routes (context ["/foo/" :foo]
151138 (ANY ["/any/" :any] [foo any]