mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
.
This commit is contained in:
parent
6ac8735245
commit
8a48d6790b
3 changed files with 15 additions and 7 deletions
|
|
@ -145,7 +145,7 @@ We can query the compiled middleware chain for the routes:
|
||||||
:result :post :middleware
|
:result :post :middleware
|
||||||
(->> (mapv :name)))
|
(->> (mapv :name)))
|
||||||
; [::mw/coerce-exceptions
|
; [::mw/coerce-exceptions
|
||||||
; ::mw/coerce-parameters
|
; ::mw/coerce-request
|
||||||
; ::mw/coerce-response]
|
; ::mw/coerce-response]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
"Middleware for pluggable request coercion.
|
"Middleware for pluggable request coercion.
|
||||||
Expects a :coercion of type `reitit.coercion/Coercion`
|
Expects a :coercion of type `reitit.coercion/Coercion`
|
||||||
and :parameters from route data, otherwise does not mount."
|
and :parameters from route data, otherwise does not mount."
|
||||||
{:name ::coerce-parameters
|
{:name ::coerce-request
|
||||||
:compile (fn [{:keys [coercion parameters]} opts]
|
:compile (fn [{:keys [coercion parameters]} opts]
|
||||||
(if (and coercion parameters)
|
(if (and coercion parameters)
|
||||||
(let [coercers (coercion/request-coercers coercion parameters opts)]
|
(let [coercers (coercion/request-coercers coercion parameters opts)]
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
[muuntaja.core :as m]
|
[muuntaja.core :as m]
|
||||||
[muuntaja.format.jsonista :as jsonista-format]
|
[muuntaja.format.jsonista :as jsonista-format]
|
||||||
[jsonista.core :as j]
|
[jsonista.core :as j]
|
||||||
[reitit.coercion-middleware :as coercion-middleware]
|
[reitit.ring.coercion-middleware :as coercion-middleware]
|
||||||
[reitit.coercion.spec :as spec]
|
[reitit.coercion.spec :as spec]
|
||||||
[reitit.coercion.schema :as schema]
|
[reitit.coercion.schema :as schema]
|
||||||
[reitit.coercion :as coercion]
|
[reitit.coercion :as coercion]
|
||||||
|
|
@ -43,7 +43,7 @@
|
||||||
;; 4600ns
|
;; 4600ns
|
||||||
(bench!
|
(bench!
|
||||||
"coerce-parameters"
|
"coerce-parameters"
|
||||||
(#'coercion-middleware/coerce-parameters coercers request))
|
(#'coercion-middleware/coerce-request-middleware coercers request))
|
||||||
|
|
||||||
;; 2700ns
|
;; 2700ns
|
||||||
(bench!
|
(bench!
|
||||||
|
|
@ -87,7 +87,7 @@
|
||||||
(-open-model [_ spec] spec)
|
(-open-model [_ spec] spec)
|
||||||
(-encode-error [_ error] error)
|
(-encode-error [_ error] error)
|
||||||
(-request-coercer [_ type spec] (fn [value format] value))
|
(-request-coercer [_ type spec] (fn [value format] value))
|
||||||
(-response-coercer [this spec] (protocol/request-coercer this :response spec)))
|
(-response-coercer [this spec] (coercion/request-coercer this :response spec {})))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(doseq [coercion [nil (->NoOpCoercion) spec/coercion]]
|
(doseq [coercion [nil (->NoOpCoercion) spec/coercion]]
|
||||||
|
|
@ -172,11 +172,15 @@
|
||||||
(cc/quick-bench (app req)))))
|
(cc/quick-bench (app req)))))
|
||||||
|
|
||||||
(defn json-perf-test []
|
(defn json-perf-test []
|
||||||
|
(title "json")
|
||||||
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
||||||
app (ring/ring-handler
|
app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:handler (fn [{{:keys [x y]} :body-params}]
|
["/plus" {:post {:handler (fn [request]
|
||||||
{:status 200, :body {:result (+ x y)}})}}]
|
(let [body (:body-params request)
|
||||||
|
x (:x body)
|
||||||
|
y (:y body)]
|
||||||
|
{:status 200, :body {:result (+ x y)}}))}}]
|
||||||
{:data {:middleware [[mm/wrap-format m]]}}))
|
{:data {:middleware [[mm/wrap-format m]]}}))
|
||||||
request {:request-method :post
|
request {:request-method :post
|
||||||
:uri "/plus"
|
:uri "/plus"
|
||||||
|
|
@ -191,6 +195,7 @@
|
||||||
(-> request app :body slurp))))
|
(-> request app :body slurp))))
|
||||||
|
|
||||||
(defn schema-json-perf-test []
|
(defn schema-json-perf-test []
|
||||||
|
(title "schema-json")
|
||||||
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
(let [m (m/create (jsonista-format/with-json-format m/default-options))
|
||||||
app (ring/ring-handler
|
app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
|
|
@ -216,6 +221,7 @@
|
||||||
(-> request app :body slurp))))
|
(-> request app :body slurp))))
|
||||||
|
|
||||||
(defn schema-perf-test []
|
(defn schema-perf-test []
|
||||||
|
(title "schema")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
["/plus" {:post {:responses {200 {:schema {:result Long}}}
|
||||||
|
|
@ -239,6 +245,7 @@
|
||||||
(call))))
|
(call))))
|
||||||
|
|
||||||
(defn data-spec-perf-test []
|
(defn data-spec-perf-test []
|
||||||
|
(title "data-spec")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema {:result int?}}}
|
["/plus" {:post {:responses {200 {:schema {:result int?}}}
|
||||||
|
|
@ -267,6 +274,7 @@
|
||||||
(s/def ::response (s/keys :req-un [::result]))
|
(s/def ::response (s/keys :req-un [::result]))
|
||||||
|
|
||||||
(defn spec-perf-test []
|
(defn spec-perf-test []
|
||||||
|
(title "spec")
|
||||||
(let [app (ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/plus" {:post {:responses {200 {:schema ::response}}
|
["/plus" {:post {:responses {200 {:schema ::response}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue