diff --git a/examples/http/README.md b/examples/http/README.md index 7ab378d6..fb1b0de2 100644 --- a/examples/http/README.md +++ b/examples/http/README.md @@ -10,8 +10,10 @@ Go with browser to: * http://localhost:3000/api/sync - synchronous -* http://localhost:3000/api/async - with core.async -* http://localhost:3000/api/future - with futures +* http://localhost:3000/api/async - with [core.async](https://github.com/clojure/core.async) channels +* http://localhost:3000/api/future - with [futures](https://clojuredocs.org/clojure.core/future) +* http://localhost:3000/api/deferred - with [manifold](https://github.com/ztellman/manifold) deferred +* http://localhost:3000/api/promesa - with [promesa](http://funcool.github.io/promesa/latest/) promises ## License diff --git a/examples/http/project.clj b/examples/http/project.clj index 4fdf5d10..52558cfd 100644 --- a/examples/http/project.clj +++ b/examples/http/project.clj @@ -2,6 +2,8 @@ :description "Reitit Ring App with Swagger" :dependencies [[org.clojure/clojure "1.9.0"] [org.clojure/core.async "0.4.474"] + [funcool/promesa "1.9.0"] + [manifold "0.1.8"] [ring "1.6.3"] [metosin/reitit "0.2.0-alpha1"]] :repl-options {:init-ns example.server}) diff --git a/examples/http/src/example/server.clj b/examples/http/src/example/server.clj index f629a08b..d3ae7a3d 100644 --- a/examples/http/src/example/server.clj +++ b/examples/http/src/example/server.clj @@ -1,46 +1,58 @@ (ns example.server (:require [reitit.http :as http] [reitit.ring :as ring] - [clojure.core.async :as a] [reitit.interceptor.sieppari] - [ring.adapter.jetty :as jetty])) + [ring.adapter.jetty :as jetty] + [clojure.core.async :as a] + [manifold.deferred :as d] + [promesa.core :as p])) -(defn -interceptor [f x] +(defn interceptor [f x] {:enter (fn [ctx] (f (update-in ctx [:request :via] (fnil conj []) x))) :leave (fn [ctx] (f (update-in ctx [:response :body] str "\n<- " x)))}) -(def interceptor (partial -interceptor identity)) -(def future-interceptor (partial -interceptor #(future %))) -(def async-interceptor (partial -interceptor #(a/go %))) +(defn handler [f] + (fn [{:keys [via]}] + (f {:status 200, + :body (str (apply str (map #(str "-> " % "\n") via)) " hello!")}))) -(defn -handler [f {:keys [via]}] - (f {:status 200, - :body (str (apply str (map #(str "-> " % "\n") via)) " hello!")})) - -(def handler (partial -handler identity)) -(def future-handler (partial -handler #(future %))) -(def async-handler (partial -handler #(a/go %))) +(def identity) +(def #(future %)) +(def #(a/go %)) +(def d/success-deferred) +(def p/promise) (def app (http/ring-handler (http/router ["/api" - {:interceptors [(interceptor :api)]} + {:interceptors [(interceptor :api)]} ["/sync" - {:interceptors [(interceptor :sync)] - :get {:interceptors [(interceptor :hello)] - :handler handler}}] + {:interceptors [(interceptor :sync)] + :get {:interceptors [(interceptor :get)] + :handler (handler )}}] ["/future" - {:interceptors [(future-interceptor :future)] - :get {:interceptors [(future-interceptor :hello)] - :handler future-handler}}] + {:interceptors [(interceptor :future)] + :get {:interceptors [(interceptor :get)] + :handler (handler )}}] ["/async" - {:interceptors [(async-interceptor :async)] - :get {:interceptors [(async-interceptor :async-hello)] - :handler async-handler}}]]) + {:interceptors [(interceptor :async)] + :get {:interceptors [(interceptor :get)] + :handler (handler )}}] + + ["/deferred" + {:interceptors [(interceptor :deferred)] + :get {:interceptors [(interceptor :get)] + :handler (handler )}}] + + ["/promesa" + {:interceptors [(interceptor :promesa)] + :get {:interceptors [(interceptor :get)] + :handler (handler )}}]]) + (ring/create-default-handler) {:executor reitit.interceptor.sieppari/executor})) diff --git a/project.clj b/project.clj index b94996df..2335975f 100644 --- a/project.clj +++ b/project.clj @@ -27,7 +27,7 @@ [metosin/ring-swagger-ui "2.2.10"] [metosin/muuntaja "0.6.0-alpha5"] [metosin/jsonista "0.2.1"] - [metosin/sieppari "0.0.0-alpha4"]] + [metosin/sieppari "0.0.0-alpha5"]] :plugins [[jonase/eastwood "0.2.6"] [lein-doo "0.1.10"] @@ -64,7 +64,7 @@ [ikitommi/immutant-web "3.0.0-alpha1"] [metosin/muuntaja "0.6.0-alpha5"] [metosin/ring-swagger-ui "2.2.10"] - [metosin/sieppari "0.0.0-alpha4"] + [metosin/sieppari "0.0.0-alpha5"] [metosin/jsonista "0.2.1"] [criterium "0.4.4"] @@ -72,6 +72,10 @@ [org.clojure/tools.namespace "0.2.11"] [com.gfredericks/test.chuck "0.2.9"] + [org.clojure/core.async "0.4.474"] + [manifold "0.1.8"] + [funcool/promesa "1.9.0"] + ;; https://github.com/bensu/doo/issues/180 [fipp "0.6.12"]]} :perf {:jvm-opts ^:replace ["-server" @@ -84,7 +88,9 @@ [io.pedestal/pedestal.service "0.5.4"] [io.pedestal/pedestal.jetty "0.5.4"] [org.clojure/core.async "0.4.474"] - [metosin/sieppari "0.0.0-alpha4"] + [manifold "0.1.8"] + [funcool/promesa "1.9.0"] + [metosin/sieppari "0.0.0-alpha5"] [yada "1.2.13"] [ring/ring-defaults "0.3.2"] [ataraxy "0.4.0"]