Skip to content

Commit a8cf4d6

Browse files
committed
backport compojure.api.core
1 parent 6f98c87 commit a8cf4d6

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/compojure/api/core.clj

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
(ns compojure.api.core
22
(:require [compojure.api.meta :as meta]
3+
[compojure.api.async]
34
[compojure.api.routes :as routes]
45
[compojure.api.middleware :as mw]
56
[compojure.core :as compojure]
67
[clojure.tools.macro :as macro]))
78

8-
(defn- handle [handlers request]
9-
(some #(% request) handlers))
9+
(defn ring-handler
10+
"Creates vanilla ring-handler from any invokable thing (e.g. compojure-api route)"
11+
[handler]
12+
(fn
13+
([request] (handler request))
14+
([request respond raise] (handler request respond raise))))
1015

1116
(defn routes
1217
"Create a Ring handler by combining several handlers into one."
1318
[& handlers]
14-
(let [handlers (seq (keep identity handlers))]
15-
(routes/create nil nil {} (vec handlers) (partial handle handlers))))
19+
(let [handlers (seq (keep identity (flatten handlers)))]
20+
(routes/map->Route
21+
{:childs (vec handlers)
22+
:handler (meta/routing handlers)})))
1623

1724
(defmacro defroutes
1825
"Define a Ring handler function from a sequence of routes.
@@ -35,7 +42,7 @@
3542
not satisfying compojure.api.routes/Routing -protocol."
3643
[& handlers]
3744
(let [handlers (keep identity handlers)]
38-
(routes/create nil nil {} nil (partial handle handlers))))
45+
(routes/map->Route {:handler (meta/routing handlers)})))
3946

4047
(defmacro middleware
4148
"Wraps routes with given middlewares using thread-first macro.
@@ -67,7 +74,7 @@
6774
{:childs [handler]
6875
:handler x-handler})))
6976

70-
(defmacro context {:style/indent 2} [& args] (meta/restructure nil args {:context? true}))
77+
(defmacro context {:style/indent 2} [& args] (meta/restructure nil args {:context? true :&form &form :&env &env}))
7178

7279
(defmacro GET {:style/indent 2} [& args] (meta/restructure :get args nil))
7380
(defmacro ANY {:style/indent 2} [& args] (meta/restructure nil args nil))

src/compojure/api/meta.clj

+5
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@
239239
`(do ~@body)
240240
(reverse (partition 2 bindings))))
241241

242+
(defn routing [handlers]
243+
(if-let [handlers (seq (keep identity (flatten handlers)))]
244+
(apply compojure.core/routes handlers)
245+
(fn ([_] nil) ([_ respond _] (respond nil)))))
246+
242247
;;
243248
;; Api
244249
;;

0 commit comments

Comments
 (0)