This commit is contained in:
Tommi Reiman 2017-12-15 19:37:04 +02:00
parent 00b6b22b6e
commit 6ac8735245
9 changed files with 23 additions and 41 deletions

View file

@ -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

View file

@ -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

View file

@ -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`

View file

@ -20,7 +20,7 @@
;;
(defn h [path]
(fn [req]
(fn [_]
{:status 200, :body path}))
(defn add [handler routes route]

View file

@ -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`

View file

@ -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]))

View file

@ -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`

View file

@ -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))