From 9729a7424ee024c6e6c995f236531dfb2f93ce66 Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 27 Aug 2018 09:12:09 +0300 Subject: [PATCH] update example --- examples/ring-example/README.md | 9 +++--- examples/ring-example/project.clj | 4 +-- examples/ring-example/src/example/dspec.clj | 3 +- examples/ring-example/src/example/plain.clj | 10 ++++++ examples/ring-example/src/example/schema.clj | 3 +- examples/ring-example/src/example/server.clj | 32 ++++++++++---------- examples/ring-example/src/example/spec.clj | 3 +- 7 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 examples/ring-example/src/example/plain.clj diff --git a/examples/ring-example/README.md b/examples/ring-example/README.md index 96df02cd..bc24ab95 100644 --- a/examples/ring-example/README.md +++ b/examples/ring-example/README.md @@ -6,15 +6,16 @@ A Sample project with ring. ```clj > lein repl - -(require '[example.server :as server]) - -(server/restart) +(start) ``` To test the endpoints using [httpie](https://httpie.org/): ```bash +# Plain +http GET :3000/plain/plus x==1 y==20 +http POST :3000/plain/plus x:=1 y:=20 + # Schema http GET :3000/schema/plus x==1 y==20 http POST :3000/schema/plus x:=1 y:=20 diff --git a/examples/ring-example/project.clj b/examples/ring-example/project.clj index 670b8b0a..47b0a65c 100644 --- a/examples/ring-example/project.clj +++ b/examples/ring-example/project.clj @@ -2,5 +2,5 @@ :description "Reitit Ring App" :dependencies [[org.clojure/clojure "1.9.0"] [ring "1.6.3"] - [metosin/muuntaja "0.4.1"] - [metosin/reitit "0.2.0-SNAPSHOT"]]) + [metosin/reitit "0.2.0-SNAPSHOT"]] + :repl-options {:init-ns example.server}) diff --git a/examples/ring-example/src/example/dspec.clj b/examples/ring-example/src/example/dspec.clj index 0c8ffa03..57cf9607 100644 --- a/examples/ring-example/src/example/dspec.clj +++ b/examples/ring-example/src/example/dspec.clj @@ -3,8 +3,7 @@ (def routes ["/dspec" {:coercion reitit.coercion.spec/coercion} - ["/plus" {:name ::plus - :responses {200 {:body {:total int?}}} + ["/plus" {:responses {200 {:body {:total int?}}} :get {:summary "plus with query-params" :parameters {:query {:x int?, :y int?}} :handler (fn [{{{:keys [x y]} :query} :parameters}] diff --git a/examples/ring-example/src/example/plain.clj b/examples/ring-example/src/example/plain.clj new file mode 100644 index 00000000..b481a5a0 --- /dev/null +++ b/examples/ring-example/src/example/plain.clj @@ -0,0 +1,10 @@ +(ns example.plain) + +(def routes + ["/plain" + ["/plus" {:get (fn [{{:strs [x y]} :query-params :as req}] + {:status 200 + :body {:total (+ (Long/parseLong x) (Long/parseLong y))}}) + :post (fn [{{:keys [x y]} :body-params}] + {:status 200 + :body {:total (+ x y)}})}]]) diff --git a/examples/ring-example/src/example/schema.clj b/examples/ring-example/src/example/schema.clj index 13ac4125..06a3434e 100644 --- a/examples/ring-example/src/example/schema.clj +++ b/examples/ring-example/src/example/schema.clj @@ -4,8 +4,7 @@ (def routes ["/schema" {:coercion reitit.coercion.schema/coercion} - ["/plus" {:name ::plus - :responses {200 {:body {:total s/Int}}} + ["/plus" {:responses {200 {:body {:total s/Int}}} :get {:summary "plus with query-params" :parameters {:query {:x s/Int, :y s/Int}} :handler (fn [{{{:keys [x y]} :query} :parameters}] diff --git a/examples/ring-example/src/example/server.clj b/examples/ring-example/src/example/server.clj index 0181c606..27483d90 100644 --- a/examples/ring-example/src/example/server.clj +++ b/examples/ring-example/src/example/server.clj @@ -1,33 +1,33 @@ (ns example.server (:require [ring.adapter.jetty :as jetty] - [ring.middleware.params] - [muuntaja.middleware] - [reitit.ring :as ring] + [ring.middleware.params :as params] + [reitit.ring.middleware.muuntaja :as muuntaja] + [muuntaja.core :as m] [reitit.ring.coercion :as coercion] + [reitit.ring :as ring] + [example.plain] [example.dspec] [example.schema] [example.spec])) -(defonce ^:private server (atom nil)) - (def app (ring/ring-handler (ring/router - [example.schema/routes + [example.plain/routes + example.schema/routes example.dspec/routes example.spec/routes] - {:data {:middleware [ring.middleware.params/wrap-params - muuntaja.middleware/wrap-format + {:data {:muuntaja m/instance + :middleware [params/wrap-params + muuntaja/format-middleware coercion/coerce-exceptions-middleware coercion/coerce-request-middleware - coercion/coerce-response-middleware]}}))) + coercion/coerce-response-middleware]}}) + (ring/create-default-handler))) -(defn restart [] - (swap! server (fn [x] - (when x (.stop x)) - (jetty/run-jetty - app - {:port 3000, :join? false}))) +(defn start [] + (jetty/run-jetty #'app {:port 3000, :join? false}) (println "server running in port 3000")) -(restart) +(comment + (start)) diff --git a/examples/ring-example/src/example/spec.clj b/examples/ring-example/src/example/spec.clj index 61a43020..49f57a6e 100644 --- a/examples/ring-example/src/example/spec.clj +++ b/examples/ring-example/src/example/spec.clj @@ -10,8 +10,7 @@ (def routes ["/spec" {:coercion reitit.coercion.spec/coercion} - ["/plus" {:name ::plus - :responses {200 {:body (s/keys :req-un [::total])}} + ["/plus" {:responses {200 {:body (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}]