diff --git a/examples/just-coercion-with-ring/src/example/dspec.clj b/examples/just-coercion-with-ring/src/example/dspec.clj index f5c343b2..8e11391c 100644 --- a/examples/just-coercion-with-ring/src/example/dspec.clj +++ b/examples/just-coercion-with-ring/src/example/dspec.clj @@ -1,6 +1,6 @@ (ns example.dspec (:require [reitit.coercion.spec :as spec-coercion] - [example.server :as server])) + [example.middleware :as middleware])) (defn handler [{{{:keys [x y]} :query} :parameters}] {:status 200 @@ -9,6 +9,6 @@ (def app (-> #'handler - (server/wrap-coercion + (middleware/wrap-coercion {:parameters {:query {:x int?, :y int?}} :coercion spec-coercion/coercion}))) diff --git a/examples/just-coercion-with-ring/src/example/middleware.clj b/examples/just-coercion-with-ring/src/example/middleware.clj new file mode 100644 index 00000000..e228d902 --- /dev/null +++ b/examples/just-coercion-with-ring/src/example/middleware.clj @@ -0,0 +1,18 @@ +(ns example.middleware + (:require [muuntaja.middleware] + [ring.middleware.params] + [reitit.middleware :as middleware] + [reitit.ring.coercion :as rrc])) + +;; unlift Middleware Record into vanilla Ring middlewareL +;; NOTE: to support format-based body coercion, an options map needs +;; to be set with :extract-request-format and extract-response-format +(defn wrap-coercion [handler resource] + (middleware/chain + [rrc/coerce-exceptions-middleware + rrc/coerce-request-middleware + rrc/coerce-response-middleware] + handler + resource + {:extract-request-format (comp :format :muuntaja/request) + :extract-response-format (comp :format :muuntaja/response)})) diff --git a/examples/just-coercion-with-ring/src/example/naive.clj b/examples/just-coercion-with-ring/src/example/naive.clj index 5fa8569d..b51f50e4 100644 --- a/examples/just-coercion-with-ring/src/example/naive.clj +++ b/examples/just-coercion-with-ring/src/example/naive.clj @@ -1,7 +1,6 @@ (ns example.naive (:require [clojure.spec.alpha :as s] - [clojure.walk :as walk] - [example.server :as server])) + [clojure.walk :as walk])) (s/def ::x int?) (s/def ::y int?) diff --git a/examples/just-coercion-with-ring/src/example/schema.clj b/examples/just-coercion-with-ring/src/example/schema.clj index 307ada8d..f1e443e3 100644 --- a/examples/just-coercion-with-ring/src/example/schema.clj +++ b/examples/just-coercion-with-ring/src/example/schema.clj @@ -1,6 +1,6 @@ (ns example.schema (:require [reitit.coercion.schema :as schema-coercion] - [example.server :as server])) + [example.middleware :as middleware])) (defn handler [{{{:keys [x y]} :query} :parameters}] {:status 200 @@ -9,6 +9,6 @@ (def app (-> #'handler - (server/wrap-coercion + (middleware/wrap-coercion {:parameters {:query {:x Long, :y Long}} :coercion schema-coercion/coercion}))) diff --git a/examples/just-coercion-with-ring/src/example/server.clj b/examples/just-coercion-with-ring/src/example/server.clj index 3697012c..f4cc5919 100644 --- a/examples/just-coercion-with-ring/src/example/server.clj +++ b/examples/just-coercion-with-ring/src/example/server.clj @@ -1,23 +1,10 @@ (ns example.server (:require [ring.adapter.jetty :as jetty] [muuntaja.middleware] - [ring.middleware.params] - [reitit.middleware :as middleware] - [reitit.ring.coercion :as rrc])) + [ring.middleware.params])) (defonce ^:private server (atom nil)) -;; unlift Middleware Record into vanilla Ring middleware -;; NOTE: to support format-based body coercion, an options map needs -;; to be set with :extract-request-format and extract-response-format -(defn wrap-coercion [handler resource] - (middleware/chain - [rrc/coerce-exceptions-middleware - rrc/coerce-request-middleware - rrc/coerce-response-middleware] - handler - resource)) - (defn restart [handler] (let [app (-> handler (ring.middleware.params/wrap-params) @@ -25,6 +12,6 @@ (swap! server (fn [x] (when x (.stop x)) (jetty/run-jetty - handler + app {:port 3000, :join? false}))) (println "server running in port 3000"))) diff --git a/examples/just-coercion-with-ring/src/example/spec.clj b/examples/just-coercion-with-ring/src/example/spec.clj index 7ff94f03..ae4d28ed 100644 --- a/examples/just-coercion-with-ring/src/example/spec.clj +++ b/examples/just-coercion-with-ring/src/example/spec.clj @@ -2,7 +2,7 @@ (:require [clojure.spec.alpha :as s] [spec-tools.spec :as spec] [reitit.coercion.spec :as spec-coercion] - [example.server :as server])) + [example.middleware :as middleware])) ;; wrap into Spec Records to enable runtime conforming (s/def ::x spec/int?) @@ -17,6 +17,6 @@ (def app (-> #'handler - (server/wrap-coercion + (middleware/wrap-coercion {:parameters {:query ::request} :coercion spec-coercion/coercion})))