diff --git a/doc/coercion/coercion.md b/doc/coercion/coercion.md index 7d7e4d21..52d66320 100644 --- a/doc/coercion/coercion.md +++ b/doc/coercion/coercion.md @@ -138,7 +138,7 @@ We get the coerced paremeters back. If a coercion fails, a typed (`:reitit.coerc ## Full example -Here's an full example for doing both routing and coercion with Reitit: +Here's an full example for doing routing and coercion with Reitit and Schema: ```clj (require '[reitit.coercion.schema]) @@ -179,9 +179,6 @@ For a full-blown http-coercion, see the [ring coercion](../ring/coercion.md). ## Thanks to -Most of the thing are just polished version of the original implementations. Thanks to: - * [compojure-api](https://clojars.org/metosin/compojure-api) for the initial `Coercion` protocol -* [ring-swagger](https://github.com/metosin/ring-swagger#more-complete-example) for the `:parameters` and `:responses` syntax. * [schema](https://github.com/plumatic/schema) and [schema-tools](https://github.com/metosin/schema-tools) for Schema Coercion * [spec-tools](https://github.com/metosin/spec-tools) for Spec Coercion diff --git a/doc/ring/coercion.md b/doc/ring/coercion.md index 62f3eff6..e9291c9d 100644 --- a/doc/ring/coercion.md +++ b/doc/ring/coercion.md @@ -52,10 +52,17 @@ Defining a coercion for a route data doesn't do anything, as it's just data. We * `coerce-response-middleware` for the response coercion * `coerce-exceptions-middleware` to turn coercion exceptions into pretty responses -### Example with Schema +### Full example + +Here's an full example for applying coercion with Reitit, Ring and Schema: ```clj (require '[reitit.ring.coercion-middleware :as mw]) +(require '[reitit.coercion.schema]) +(require '[reitit.ring :as ring]) +(require '[schema.core :as s]) + +(def PositiveInt (s/constrained s/Int pos? 'PositiveInt)) (def app (ring/ring-handler @@ -126,7 +133,7 @@ Invalid response: ### Optimizations -The coercion middleware are [compiled againts a route](compiling_middleware,md). This enables them to compile and cache the actual coercers for the defined models ahead of time. They also unmount if a route doesn't have `:coercion` and `:parameters` or `:responses` defined. +The coercion middleware are [compiled againts a route](compiling_middleware,md). In the compile step the actual coercer implementations are compiled for the defined models. Also, the mw doesn't mount itself if a route doesn't have `:coercion` and `:parameters` or `:responses` defined. We can query the compiled middleware chain for the routes: @@ -160,8 +167,6 @@ Has no mounted middleware: ``` ## Thanks to -Most of the thing are just polished version of the original implementations. Thanks to: - * [compojure-api](https://clojars.org/metosin/compojure-api) for the initial `Coercion` protocol * [ring-swagger](https://github.com/metosin/ring-swagger#more-complete-example) for the `:parameters` and `:responses` syntax. * [schema](https://github.com/plumatic/schema) and [schema-tools](https://github.com/metosin/schema-tools) for Schema Coercion diff --git a/perf-test/clj/reitit/coercion_perf_test.clj b/perf-test/clj/reitit/coercion_perf_test.clj index d3641446..2b496374 100644 --- a/perf-test/clj/reitit/coercion_perf_test.clj +++ b/perf-test/clj/reitit/coercion_perf_test.clj @@ -4,7 +4,6 @@ [reitit.perf-utils :refer :all] [clojure.spec.alpha :as s] [spec-tools.core :as st] - [spec-tools.data-spec :as ds] [muuntaja.middleware :as mm] [muuntaja.core :as m] [muuntaja.format.jsonista :as jsonista-format] @@ -13,9 +12,7 @@ [reitit.coercion.spec :as spec] [reitit.coercion.schema :as schema] [reitit.coercion :as coercion] - [reitit.ring :as ring] - [reitit.core :as r]) - (:import (java.io ByteArrayInputStream))) + [reitit.ring :as ring])) ;; ;; start repl with `lein perf repl` diff --git a/perf-test/clj/reitit/go_perf_test.clj b/perf-test/clj/reitit/go_perf_test.clj index 676c99a2..5c5dd175 100644 --- a/perf-test/clj/reitit/go_perf_test.clj +++ b/perf-test/clj/reitit/go_perf_test.clj @@ -20,7 +20,7 @@ ;; (defn h [path] - (fn [req] + (fn [_] {:status 200, :body path})) (defn add [handler routes route] diff --git a/perf-test/clj/reitit/lupapiste_perf_test.clj b/perf-test/clj/reitit/lupapiste_perf_test.clj index dff097ce..9d44b02b 100644 --- a/perf-test/clj/reitit/lupapiste_perf_test.clj +++ b/perf-test/clj/reitit/lupapiste_perf_test.clj @@ -1,22 +1,15 @@ (ns reitit.lupapiste-perf-test (:require [clojure.test :refer [deftest testing is]] - [criterium.core :as cc] [reitit.perf-utils :refer :all] - [cheshire.core :as json] - [clojure.string :as str] [reitit.core :as reitit] [reitit.ring :as ring] [bidi.bidi :as bidi] - - [ataraxy.core :as ataraxy] - [compojure.core :as compojure] [io.pedestal.http.route.definition.table :as table] [io.pedestal.http.route.map-tree :as map-tree] - [io.pedestal.http.route.router :as pedestal] - [io.pedestal.http.route :as route])) + [io.pedestal.http.route.router :as pedestal])) ;; ;; start repl with `lein perf repl` diff --git a/perf-test/clj/reitit/nodejs_perf_test.clj b/perf-test/clj/reitit/nodejs_perf_test.clj index fd31d31b..512a421f 100644 --- a/perf-test/clj/reitit/nodejs_perf_test.clj +++ b/perf-test/clj/reitit/nodejs_perf_test.clj @@ -1,6 +1,5 @@ (ns reitit.nodejs-perf-test - (:require [criterium.core :as cc] - [reitit.perf-utils :refer :all] + (:require [reitit.perf-utils :refer :all] [immutant.web :as web] [reitit.ring :as ring])) diff --git a/perf-test/clj/reitit/opensensors_perf_test.clj b/perf-test/clj/reitit/opensensors_perf_test.clj index 05f0c268..2beba938 100644 --- a/perf-test/clj/reitit/opensensors_perf_test.clj +++ b/perf-test/clj/reitit/opensensors_perf_test.clj @@ -1,22 +1,17 @@ (ns reitit.opensensors-perf-test - (:require [clojure.test :refer [deftest testing is]] - [criterium.core :as cc] - [reitit.perf-utils :refer :all] + (:require [reitit.perf-utils :refer :all] [cheshire.core :as json] [clojure.string :as str] [reitit.core :as reitit] [reitit.ring :as ring] [bidi.bidi :as bidi] - [ataraxy.core :as ataraxy] - [compojure.core :refer [routes context ANY]] [io.pedestal.http.route.definition.table :as table] [io.pedestal.http.route.map-tree :as map-tree] - [io.pedestal.http.route.router :as pedestal] - [io.pedestal.http.route :as route])) + [io.pedestal.http.route.router :as pedestal])) ;; ;; start repl with `lein perf repl` diff --git a/perf-test/clj/reitit/perf_utils.clj b/perf-test/clj/reitit/perf_utils.clj index 4ce4b33b..deb7b499 100644 --- a/perf-test/clj/reitit/perf_utils.clj +++ b/perf-test/clj/reitit/perf_utils.clj @@ -30,11 +30,7 @@ (defn bench-routes [routes req f] (let [router (reitit/router routes) - urls (valid-urls router) - random-url #(rand-nth urls) - log-time #(let [now (System/nanoTime)] (%) (- (System/nanoTime) now)) - total 10000 - dropped (int (* total 0.45))] + urls (valid-urls router)] (mapv (fn [path] (let [request (map->Request (req path)) diff --git a/perf-test/clj/reitit/prefix_tree_perf_test.clj b/perf-test/clj/reitit/prefix_tree_perf_test.clj index 1f09421e..f920aadf 100644 --- a/perf-test/clj/reitit/prefix_tree_perf_test.clj +++ b/perf-test/clj/reitit/prefix_tree_perf_test.clj @@ -70,10 +70,10 @@ nil routes)) #_(def reitit-tree - (reduce - (fn [acc [p d]] - (trie/insert acc p d)) - nil routes)) + (reduce + (fn [acc [p d]] + (trie/insert acc p d)) + nil routes)) (def reitit-segment (segment/create routes)) @@ -101,8 +101,8 @@ ;; 0.8ms (return route-data) ;; 0.8ms (fix payloads) #_(cc/quick-bench - (dotimes [_ 1000] - (trie/lookup reitit-tree "/v1/orgs/1/topics" {}))) + (dotimes [_ 1000] + (trie/lookup reitit-tree "/v1/orgs/1/topics" {}))) ;; 0.9ms (initial) ;; 0.5ms (protocols)