-
Notifications
You must be signed in to change notification settings - Fork 150
Async
Tommi Reiman edited this page Jun 13, 2017
·
11 revisions
[metosin/compojure-api "2.0.0-alpha2"]
Compojure-api supports the new async ring.
- Endpoints can return either manifold
Deferred
or core.asyncManyToManyChannel
s. - Endpoints and Middleware can be invoked with CPS-style enabling custom async.
(require '[compojure.api.sweet :refer [GET]])
(require '[ring.util.http-response :refer [ok]])
(require '[manifold.deferred :as d])
(require '[aleph.http :as http])
(require '[schema.core :as s])
(def route
(GET "/repository/:group/:artifact/downloads" []
:summary "Async gets the clojars download count"
:path-params [group :- s/Str, artifact :- s/Str]
:return (s/maybe s/Int)
(d/chain
(http/get
(str "https://clojars.org/api/artifacts/" group "/" artifact)
{:as :json, :throw-exceptions false})
:body
:downloads
ok)))
;; sync
(route {:request-method :get, :uri "/repository/metosin/compojure-api/downloads"})
; {:body 207917 :headers {} :status 200 :compojure.api.meta/serializable? true}
;; async
(let [respond (promise)
raise (promise)]
(route {:request-method :get, :uri "/repository/metosin/compojure-api/downloads"} respond raise)
(deref respond 2000 ::timeout))
; {:body 207917 :headers {} :status 200 :compojure.api.meta/serializable? true}
TODO
Ring-adapters (for different web-servers) need to support the async ring. Here's a list of adapters supporting that currently:
Should work, need manual bootstrapping:
- Aleph
- HttpKit
- Immutant