diff --git a/examples/ring-example/.gitignore b/examples/ring-example/.gitignore new file mode 100644 index 00000000..c53038ec --- /dev/null +++ b/examples/ring-example/.gitignore @@ -0,0 +1,11 @@ +/target +/classes +/checkouts +pom.xml +pom.xml.asc +*.jar +*.class +/.lein-* +/.nrepl-port +.hgignore +.hg/ diff --git a/examples/ring-example/README.md b/examples/ring-example/README.md new file mode 100644 index 00000000..a3811b30 --- /dev/null +++ b/examples/ring-example/README.md @@ -0,0 +1,33 @@ +# Ring example + +A Sample project with ring. + +## Usage + +```clj +> lein repl + +(require '[example.server :as server]) + +(server/restart) +``` + +To test the endpoints using [httpie](https://httpie.org/): + +```bash +# Schema +http GET :3000/schema/plus x==1 y==20 +http POST :3000/schema/plus x:=1 y:=20 + +# Data-specs +http GET :3000/dspec/plus x==1 y==20 +http POST :3000/dspec/plus x:=1 y:=20 + +# Specs +http GET :3000/spec/plus x==1 y==20 +http POST :3000/spec/plus x:=1 y:=20 +``` + +## License + +Copyright © 2017 Metosin Oy diff --git a/examples/ring-example/project.clj b/examples/ring-example/project.clj new file mode 100644 index 00000000..11bbb381 --- /dev/null +++ b/examples/ring-example/project.clj @@ -0,0 +1,6 @@ +(defproject just-coercion-with-ring "0.1.0-SNAPSHOT" + :description "Reitit coercion with vanilla ring" + :dependencies [[org.clojure/clojure "1.9.0-RC2"] + [ring "1.6.3"] + [metosin/muuntaja "0.4.1"] + [metosin/reitit "0.1.0-SNAPSHOT"]]) diff --git a/examples/ring-example/src/example/dspec.clj b/examples/ring-example/src/example/dspec.clj new file mode 100644 index 00000000..7c336214 --- /dev/null +++ b/examples/ring-example/src/example/dspec.clj @@ -0,0 +1,19 @@ +(ns example.dspec + (:require [reitit.ring.coercion :as coercion] + [reitit.ring.coercion.spec :as spec-coercion])) + +(def routes + ["/dspec" + ["/plus" {:name ::plus + :coercion spec-coercion/coercion + :responses {200 {:schema {:total int?}}} + :get {:summary "plus with query-params" + :parameters {:query {:x int?, :y int?}} + :handler (fn [{{{:keys [x y]} :query} :parameters}] + {:status 200 + :body {:total (+ x y)}})} + :post {:summary "plus with body-params" + :parameters {:body {:x int?, :y int?}} + :handler (fn [{{{:keys [x y]} :body} :parameters}] + {:status 200 + :body {:total (+ x y)}})}}]]) diff --git a/examples/ring-example/src/example/schema.clj b/examples/ring-example/src/example/schema.clj new file mode 100644 index 00000000..55c3179a --- /dev/null +++ b/examples/ring-example/src/example/schema.clj @@ -0,0 +1,20 @@ +(ns example.schema + (:require [schema.core :as s] + [reitit.ring.coercion :as coercion] + [reitit.ring.coercion.schema :as schema-coercion])) + +(def routes + ["/schema" + ["/plus" {:name ::plus + :coercion schema-coercion/coercion + :responses {200 {:schema {:total s/Int}}} + :get {:summary "plus with query-params" + :parameters {:query {:x s/Int, :y s/Int}} + :handler (fn [{{{:keys [x y]} :query} :parameters}] + {:status 200 + :body {:total (+ x y)}})} + :post {:summary "plus with body-params" + :parameters {:body {:x s/Int, :y s/Int}} + :handler (fn [{{{:keys [x y]} :body} :parameters}] + {:status 200 + :body {:total (+ x y)}})}}]]) diff --git a/examples/ring-example/src/example/server.clj b/examples/ring-example/src/example/server.clj new file mode 100644 index 00000000..0181c606 --- /dev/null +++ b/examples/ring-example/src/example/server.clj @@ -0,0 +1,33 @@ +(ns example.server + (:require [ring.adapter.jetty :as jetty] + [ring.middleware.params] + [muuntaja.middleware] + [reitit.ring :as ring] + [reitit.ring.coercion :as coercion] + [example.dspec] + [example.schema] + [example.spec])) + +(defonce ^:private server (atom nil)) + +(def app + (ring/ring-handler + (ring/router + [example.schema/routes + example.dspec/routes + example.spec/routes] + {:data {:middleware [ring.middleware.params/wrap-params + muuntaja.middleware/wrap-format + coercion/coerce-exceptions-middleware + coercion/coerce-request-middleware + coercion/coerce-response-middleware]}}))) + +(defn restart [] + (swap! server (fn [x] + (when x (.stop x)) + (jetty/run-jetty + app + {:port 3000, :join? false}))) + (println "server running in port 3000")) + +(restart) diff --git a/examples/ring-example/src/example/spec.clj b/examples/ring-example/src/example/spec.clj new file mode 100644 index 00000000..e6acc570 --- /dev/null +++ b/examples/ring-example/src/example/spec.clj @@ -0,0 +1,26 @@ +(ns example.spec + (:require [clojure.spec.alpha :as s] + [spec-tools.spec :as spec] + [reitit.ring.coercion :as coercion] + [reitit.ring.coercion.spec :as spec-coercion])) + +;; wrap into Spec Records to enable runtime conforming +(s/def ::x spec/int?) +(s/def ::y spec/int?) +(s/def ::total spec/int?) + +(def routes + ["/spec" + ["/plus" {:name ::plus + :coercion spec-coercion/coercion + :responses {200 {:schema (s/keys :req-un [::total])}} + :get {:summary "plus with query-params" + :parameters {:query (s/keys :req-un [::x ::y])} + :handler (fn [{{{:keys [x y]} :query} :parameters}] + {:status 200 + :body {:total (+ x y)}})} + :post {:summary "plus with body-params" + :parameters {:body (s/keys :req-un [::x ::y])} + :handler (fn [{{{:keys [x y]} :body} :parameters}] + {:status 200 + :body {:total (+ x y)}})}}]]) diff --git a/project.clj b/project.clj index e12d6f36..81402f3a 100644 --- a/project.clj +++ b/project.clj @@ -45,6 +45,7 @@ [expound "0.3.2"] [orchestra "2017.08.13"] + [ring "1.6.3"] [metosin/muuntaja "0.4.1"] [metosin/jsonista "0.1.0-SNAPSHOT"]