From c487b272888294be99a7d2eb328187bd3884e9f9 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 2 Sep 2018 19:23:18 +0300 Subject: [PATCH 1/4] initial rework --- book.json | 2 +- doc/SUMMARY.md | 97 ++++++++++++--------- doc/advanced/README.md | 6 -- doc/{patterns => advanced}/shared_routes.md | 0 doc/basics/README.md | 9 -- doc/cljdoc.edn | 45 ++++------ doc/coercion/README.md | 6 -- doc/frontend/README.md | 5 -- doc/{ => http}/interceptors.md | 0 doc/patterns/README.md | 3 - doc/ring/README.md | 15 ---- 11 files changed, 76 insertions(+), 112 deletions(-) delete mode 100644 doc/advanced/README.md rename doc/{patterns => advanced}/shared_routes.md (100%) delete mode 100644 doc/basics/README.md delete mode 100644 doc/coercion/README.md delete mode 100644 doc/frontend/README.md rename doc/{ => http}/interceptors.md (100%) delete mode 100644 doc/patterns/README.md delete mode 100644 doc/ring/README.md diff --git a/book.json b/book.json index aa1f4270..4f7a487a 100644 --- a/book.json +++ b/book.json @@ -1,6 +1,6 @@ { "root": "doc", - "plugins": ["editlink", "github", "highlight"], + "plugins": ["hints", "editlink", "github", "highlight"], "pluginsConfig": { "editlink": { "base": "https://github.com/metosin/reitit/tree/master/doc", diff --git a/doc/SUMMARY.md b/doc/SUMMARY.md index 65ae642e..e6b72f6c 100644 --- a/doc/SUMMARY.md +++ b/doc/SUMMARY.md @@ -1,46 +1,63 @@ # Summary +## Introduction + * [Introduction](README.md) -* [Basics](basics/README.md) - * [Route Syntax](basics/route_syntax.md) - * [Router](basics/router.md) - * [Path-based Routing](basics/path_based_routing.md) - * [Name-based Routing](basics/name_based_routing.md) - * [Route Data](basics/route_data.md) - * [Route Data Validation](basics/route_data_validation.md) - * [Route Conflicts](basics/route_conflicts.md) -* [Coercion](coercion/README.md) - * [Coercion Explained](coercion/coercion.md) - * [Plumatic Schema](coercion/schema_coercion.md) - * [Clojure.spec](coercion/clojure_spec_coercion.md) - * [Data-specs](coercion/data_spec_coercion.md) -* [Ring](ring/README.md) - * [Ring-router](ring/ring.md) - * [Reverse-routing](ring/reverse_routing.md) - * [Default handler](ring/default_handler.md) - * [Static Resources](ring/static.md) - * [Dynamic Extensions](ring/dynamic_extensions.md) - * [Data-driven Middleware](ring/data_driven_middleware.md) - * [Transforming Middleware Chain](ring/transforming_middleware_chain.md) - * [Middleware Registry](ring/middleware_registry.md) - * [Default Middleware](ring/default_middleware.md) - * [Pluggable Coercion](ring/coercion.md) - * [Route Data Validation](ring/route_data_validation.md) - * [Compiling Middleware](ring/compiling_middleware.md) - * [Swagger Support](ring/swagger.md) -* [Advanced](advanced/README.md) - * [Configuring Routers](advanced/configuring_routers.md) - * [Composing Routers](advanced/composing_routers.md) - * [Different Routers](advanced/different_routers.md) - * [Route Validation](advanced/route_validation.md) - * [Dev Workflow](advanced/dev_workflow.md) -* [Patterns](patterns/README.md) - * [Shared Routes](patterns/shared_routes.md) -* [Frontend](frontend/README.md) - * [Basics](frontend/basics.md) - * [Browser integration](frontend/browser.md) - * [Controllers (WIP)](frontend/controllers.md) + +## Basics + +* [Route Syntax](basics/route_syntax.md) +* [Router](basics/router.md) +* [Path-based Routing](basics/path_based_routing.md) +* [Name-based Routing](basics/name_based_routing.md) +* [Route Data](basics/route_data.md) +* [Route Data Validation](basics/route_data_validation.md) +* [Route Conflicts](basics/route_conflicts.md) + +## Coercion + +* [Coercion Explained](coercion/coercion.md) +* [Plumatic Schema](coercion/schema_coercion.md) +* [Clojure.spec](coercion/clojure_spec_coercion.md) +* [Data-specs](coercion/data_spec_coercion.md) + +## Ring + +* [Ring-router](ring/ring.md) +* [Reverse-routing](ring/reverse_routing.md) +* [Default handler](ring/default_handler.md) +* [Static Resources](ring/static.md) +* [Dynamic Extensions](ring/dynamic_extensions.md) +* [Data-driven Middleware](ring/data_driven_middleware.md) +* [Transforming Middleware Chain](ring/transforming_middleware_chain.md) +* [Middleware Registry](ring/middleware_registry.md) +* [Default Middleware](ring/default_middleware.md) +* [Pluggable Coercion](ring/coercion.md) +* [Route Data Validation](ring/route_data_validation.md) +* [Compiling Middleware](ring/compiling_middleware.md) +* [Swagger Support](ring/swagger.md) + +## Frontend + +* [Basics](frontend/basics.md) +* [Browser integration](frontend/browser.md) +* [Controllers (WIP)](frontend/controllers.md) + +## HTTP + +* [Interceptors](http/interceptors.md) + +## Advanced + +* [Configuring Routers](advanced/configuring_routers.md) +* [Composing Routers](advanced/composing_routers.md) +* [Different Routers](advanced/different_routers.md) +* [Route Validation](advanced/route_validation.md) +* [Dev Workflow](advanced/dev_workflow.md) +* [Shared Routes](advanced/shared_routes.md) + +## Misc + * [Performance](performance.md) -* [Interceptors (WIP)](interceptors.md) * [Development Instructions](development.md) * [FAQ](faq.md) diff --git a/doc/advanced/README.md b/doc/advanced/README.md deleted file mode 100644 index 6069fb9f..00000000 --- a/doc/advanced/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Advanced - -* [Configuring Routers](configuring_routers.md) -* [Composing Routers](composing_routers.md) -* [Different Routers](different_routers.md) -* [Route Validation](route_validation.md) diff --git a/doc/patterns/shared_routes.md b/doc/advanced/shared_routes.md similarity index 100% rename from doc/patterns/shared_routes.md rename to doc/advanced/shared_routes.md diff --git a/doc/basics/README.md b/doc/basics/README.md deleted file mode 100644 index 77fcb3d7..00000000 --- a/doc/basics/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Basics - -* [Route Syntax](route_syntax.md) -* [Router](router.md) -* [Path-based Routing](path_based_routing.md) -* [Name-based Routing](name_based_routing.md) -* [Route Data](route_data.md) -* [Route Data Validation](route_data_validation.md) -* [Route Conflicts](route_conflicts.md) diff --git a/doc/cljdoc.edn b/doc/cljdoc.edn index 56a81643..77dcdc49 100644 --- a/doc/cljdoc.edn +++ b/doc/cljdoc.edn @@ -9,56 +9,47 @@ :cljdoc.doc/tree [["Introduction" {:file "doc/README.md"}] ["Basics" - {:file "doc/basics/README.md"} ["Route Syntax" {:file "doc/basics/route_syntax.md"}] ["Router" {:file "doc/basics/router.md"}] ["Path-based Routing" {:file "doc/basics/path_based_routing.md"}] ["Name-based Routing" {:file "doc/basics/name_based_routing.md"}] ["Route Data" {:file "doc/basics/route_data.md"}] - ["Route Data Validation" - {:file "doc/basics/route_data_validation.md"}] + ["Route Data Validation" {:file "doc/basics/route_data_validation.md"}] ["Route Conflicts" {:file "doc/basics/route_conflicts.md"}]] ["Coercion" - {:file "doc/coercion/README.md"} ["Coercion Explained" {:file "doc/coercion/coercion.md"}] ["Plumatic Schema" {:file "doc/coercion/schema_coercion.md"}] ["Clojure.spec" {:file "doc/coercion/clojure_spec_coercion.md"}] ["Data-specs" {:file "doc/coercion/data_spec_coercion.md"}]] ["Ring" - {:file "doc/ring/README.md"} ["Ring-router" {:file "doc/ring/ring.md"}] ["Reverse-routing" {:file "doc/ring/reverse_routing.md"}] ["Default handler" {:file "doc/ring/default_handler.md"}] ["Static Resources" {:file "doc/ring/static.md"}] ["Dynamic Extensions" {:file "doc/ring/dynamic_extensions.md"}] - ["Data-driven Middleware" - {:file "doc/ring/data_driven_middleware.md"}] - ["Transforming Middleware Chain" - {:file "doc/ring/transforming_middleware_chain.md"}] + ["Data-driven Middleware" {:file "doc/ring/data_driven_middleware.md"}] + ["Transforming Middleware Chain" {:file "doc/ring/transforming_middleware_chain.md"}] ["Middleware Registry" {:file "doc/ring/middleware_registry.md"}] ["Default Middleware" {:file "doc/ring/default_middleware.md"}] ["Pluggable Coercion" {:file "doc/ring/coercion.md"}] - ["Route Data Validation" - {:file "doc/ring/route_data_validation.md"}] + ["Route Data Validation" {:file "doc/ring/route_data_validation.md"}] ["Compiling Middleware" {:file "doc/ring/compiling_middleware.md"}] ["Swagger Support" {:file "doc/ring/swagger.md"}]] - ["Advanced" - {:file "doc/advanced/README.md"} - ["Configuring Routers" - {:file "doc/advanced/configuring_routers.md"}] - ["Composing Routers" {:file "doc/advanced/composing_routers.md"}] - ["Different Routers" {:file "doc/advanced/different_routers.md"}] - ["Route Validation" {:file "doc/advanced/route_validation.md"}] - ["Dev Workflow" {:file "doc/advanced/dev_workflow.md"}]] - ["Patterns" - {:file "doc/patterns/README.md"} - ["Shared Routes" {:file "doc/patterns/shared_routes.md"}]] ["Frontend" - {:file "doc/frontend/README.md"} ["Basics" {:file "doc/frontend/basics.md"}] ["Browser integration" {:file "doc/frontend/browser.md"}] ["Controllers (WIP)" {:file "doc/frontend/controllers.md"}]] - ["Performance" {:file "doc/performance.md"}] - ["Interceptors (WIP)" {:file "doc/interceptors.md"}] - ["Development Instructions" {:file "doc/development.md"}] - ["FAQ" {:file "doc/faq.md"}]]} + ["HTTP" + ["Interceptors" {:file "doc/http/interceptors.md"}]] + ["Advanced" + ["Configuring Routers" {:file "doc/advanced/configuring_routers.md"}] + ["Composing Routers" {:file "doc/advanced/composing_routers.md"}] + ["Different Routers" {:file "doc/advanced/different_routers.md"}] + ["Route Validation" {:file "doc/advanced/route_validation.md"}] + ["Dev Workflow" {:file "doc/advanced/dev_workflow.md"}] + ["Shared Routes" {:file "doc/patterns/shared_routes.md"}]] + ["Misc" + ["Performance" {:file "doc/performance.md"}] + ["Interceptors (WIP)" {:file "doc/interceptors.md"}] + ["Development Instructions" {:file "doc/development.md"}] + ["FAQ" {:file "doc/faq.md"}]]]} diff --git a/doc/coercion/README.md b/doc/coercion/README.md deleted file mode 100644 index 2cc3d479..00000000 --- a/doc/coercion/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Coercion - -* [Coercion Explained](coercion.md) -* [Plumatic Schema](schema_coercion.md) -* [Clojure.spec](clojure_spec_coercion.md) -* [Data-specs](data_spec_coercion.md) diff --git a/doc/frontend/README.md b/doc/frontend/README.md deleted file mode 100644 index e1270493..00000000 --- a/doc/frontend/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Frontend - -* [Basics](basics.md) -* [Browser integration](browser.md) -* [Controllers (WIP)](controllers.md) diff --git a/doc/interceptors.md b/doc/http/interceptors.md similarity index 100% rename from doc/interceptors.md rename to doc/http/interceptors.md diff --git a/doc/patterns/README.md b/doc/patterns/README.md deleted file mode 100644 index 134c53c1..00000000 --- a/doc/patterns/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Patterns - -* [Shared Routes](shared_routes.md) diff --git a/doc/ring/README.md b/doc/ring/README.md deleted file mode 100644 index 814a92b6..00000000 --- a/doc/ring/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# Ring - -* [Ring-router](ring.md) -* [Reverse-routing](reverse_routing.md) -* [Default handler](default_handler.md) -* [Static Resources](static.md) -* [Dynamic Extensions](dynamic_extensions.md) -* [Data-driven Middleware](data_driven_middleware.md) -* [Transforming Middleware Chain](transforming_middleware_chain.md) -* [Middleware Registry](middleware_registry.md) -* [Default Middleware](default_middleware.md) -* [Pluggable Coercion](coercion.md) -* [Route Data Validation](route_data_validation.md) -* [Compiling Middleware](compiling_middleware.md) -* [Swagger Support](swagger.md) From b3a24f2908ff8fb543a7027a0a344819153b71a4 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Sun, 2 Sep 2018 19:40:25 +0300 Subject: [PATCH 2/4] http --- doc/http/interceptors.md | 82 +++++++++------------------------------- 1 file changed, 17 insertions(+), 65 deletions(-) diff --git a/doc/http/interceptors.md b/doc/http/interceptors.md index 011c15b6..d3ec0a2c 100644 --- a/doc/http/interceptors.md +++ b/doc/http/interceptors.md @@ -1,77 +1,29 @@ # Interceptors (WIP) -Reitit also support for [Pedestal](pedestal.io)-style [interceptors](http://pedestal.io/reference/interceptors) as an alternative to Middleware. Basic interceptor handling is implemented in `reitit.interceptor` package. There is no interceptor executor shipped, but you can use libraries like [Pedestal Interceptor](https://github.com/pedestal/pedestal/tree/master/interceptor) or [Sieppari](https://github.com/metosin/sieppari) to execute the chains. +Reitit also support for [Pedestal](pedestal.io)-style [interceptors](http://pedestal.io/reference/interceptors) as an alternative to using middleware. Basic interceptor handling is implemented in `reitit.interceptor` package. There is no interceptor executor shipped, but you can use libraries like [Pedestal Interceptor](https://github.com/pedestal/pedestal/tree/master/interceptor) or [Sieppari](https://github.com/metosin/sieppari) to execute the chains. + +## Current Status + +Work-in-progress and considered alpha quality. ## Reitit-http -An alternative to `reitit-ring`, using interceptors instead of middleware. Currently not finalized, you can track progress in [here](https://github.com/metosin/reitit/pull/124). +```clj +[metosin/reitit-http "0.2.0-alpha1"] +``` + +An module for http-routing using interceptors instead of middleware. Builds on top of the [`reitit-ring`](../ring/ring.md) module. The differences: + +* instead of `:middleware`, uses `:interceptors` +* compared to `reitit.http/http-router` takes an extra options map with mandatory key `:executor` (of type `reitit.interceptor/Executor`) and optional top level `:interceptors` - wrapping both routes and default handler. +* optional entry poitn `reitit.http/routing-interceptor` to provide a routing interceptor, to be used with Pedestal. ## Examples -### Standalone +### Sieppari -* [Sieppari](https://github.com/metosin/sieppari) for executing the chain -* [Manifold](https://github.com/ztellman/manifold) for async -* [data-specs](https://github.com/metosin/spec-tools/blob/master/README.md#data-specs) for coercion - -```clj -(require '[reitit.interceptor.sieppari :as sieppari]) -(require '[reitit.http.coercion :as coercion]) -(require '[reitit.http :as http]) -(require '[reitit.ring :as ring]) -(require '[reitit.coercion.spec]) -(require '[clojure.set :as set]) -(require '[manifold.deferred :as d]) -(require '[ring.adapter.jetty :as jetty]) - -(def auth-interceptor - "Interceptor that mounts itself if route has `:roles` data. Expects `:roles` - to be a set of keyword and the context to have `[:user :roles]` with user roles. - responds with HTTP 403 if user doesn't have the roles defined, otherwise no-op." - {:name ::auth - :compile (fn [{:keys [roles]} _] - (if (seq roles) - {:description (str "requires roles " roles) - :spec {:roles #{keyword?}} - :context-spec {:user {:roles #{keyword}}} - :enter (fn [{{user-roles :roles} :user :as ctx}] - (if (not (set/subset? roles user-roles)) - (assoc ctx :response {:status 403, :body "forbidden"}) - ctx))}))}) - -(def async-interceptor - {:enter (fn [ctx] (d/future ctx))}) - -(def app - (http/ring-handler - (http/router - ["/api" {:interceptors [async-interceptor auth-interceptor]} - ["/ping" {:name ::ping - :get (constantly - {:status 200 - :body "pong"})}] - ["/plus/:z" {:name ::plus - :post {:parameters {:query {:x int?} - :body {:y int?} - :path {:z int?}} - :responses {200 {:body {:total pos-int?}}} - :roles #{:admin} - :handler (fn [{:keys [parameters]}] - (let [total (+ (-> parameters :query :x) - (-> parameters :body :y) - (-> parameters :path :z))] - {:status 200 - :body {:total total}}))}}]] - {:data {:coercion reitit.coercion.spec/coercion - :interceptors [coercion/coerce-exceptions-interceptor - coercion/coerce-request-interceptor - coercion/coerce-response-interceptor]}}) - (ring/create-default-handler) - {:executor sieppari/executor})) - -(jetty/run-jetty #'app {:port 3000, :join? false, :async? true}) -``` +See code at: https://github.com/metosin/reitit/tree/master/examples/http ### Pedestal -**TODO** +See example at: https://github.com/metosin/reitit/tree/master/examples/pedestal From 13ceab524d926445373d2e41e53c0febc2596ff9 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 3 Sep 2018 12:35:28 +0300 Subject: [PATCH 3/4] sieppari 0.0.0-alpha5 --- examples/http/README.md | 6 ++- examples/http/project.clj | 2 + examples/http/src/example/server.clj | 58 +++++++++++++++++----------- project.clj | 12 ++++-- 4 files changed, 50 insertions(+), 28 deletions(-) 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"] From 048f7e48b64e1fb59fa9edb5cc707fb2a42d61eb Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 3 Sep 2018 18:52:57 +0300 Subject: [PATCH 4/4] return json --- examples/http/src/example/server.clj | 10 ++++++---- examples/ring-swagger/src/example/server.clj | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/http/src/example/server.clj b/examples/http/src/example/server.clj index d3ae7a3d..adc6a90d 100644 --- a/examples/http/src/example/server.clj +++ b/examples/http/src/example/server.clj @@ -3,18 +3,19 @@ [reitit.ring :as ring] [reitit.interceptor.sieppari] [ring.adapter.jetty :as jetty] + [muuntaja.interceptor] [clojure.core.async :as a] [manifold.deferred :as d] [promesa.core :as p])) (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)))}) + {:enter (fn [ctx] (f (update-in ctx [:request :via] (fnil conj []) {:enter x}))) + :leave (fn [ctx] (f (update-in ctx [:response :body] conj {:leave x})))}) (defn handler [f] (fn [{:keys [via]}] (f {:status 200, - :body (str (apply str (map #(str "-> " % "\n") via)) " hello!")}))) + :body (conj via :handler)}))) (def identity) (def #(future %)) @@ -54,7 +55,8 @@ :handler (handler )}}]]) (ring/create-default-handler) - {:executor reitit.interceptor.sieppari/executor})) + {:executor reitit.interceptor.sieppari/executor + :interceptors [(muuntaja.interceptor/format-interceptor)]})) (defn start [] (jetty/run-jetty #'app {:port 3000, :join? false, :async? true}) diff --git a/examples/ring-swagger/src/example/server.clj b/examples/ring-swagger/src/example/server.clj index 50f856db..19e36257 100644 --- a/examples/ring-swagger/src/example/server.clj +++ b/examples/ring-swagger/src/example/server.clj @@ -26,10 +26,11 @@ ["/upload" {:post {:summary "upload a file" :parameters {:multipart {:file multipart/temp-file-part}} - :responses {200 {:body {:file multipart/temp-file-part}}} + :responses {200 {:body {:name string?, :size int?}}} :handler (fn [{{{:keys [file]} :multipart} :parameters}] {:status 200 - :body {:file file}})}}] + :body {:name (:filename file) + :size (:size file)}})}}] ["/download" {:get {:summary "downloads a file" @@ -37,7 +38,8 @@ :handler (fn [_] {:status 200 :headers {"Content-Type" "image/png"} - :body (io/input-stream (io/resource "reitit.png"))})}}]] + :body (io/input-stream + (io/resource "reitit.png"))})}}]] ["/math" {:swagger {:tags ["math"]}}