update example

This commit is contained in:
Tommi Reiman 2018-08-27 09:12:09 +03:00
parent 218db83267
commit 9729a7424e
7 changed files with 36 additions and 28 deletions

View file

@ -6,15 +6,16 @@ A Sample project with ring.
```clj ```clj
> lein repl > lein repl
(start)
(require '[example.server :as server])
(server/restart)
``` ```
To test the endpoints using [httpie](https://httpie.org/): To test the endpoints using [httpie](https://httpie.org/):
```bash ```bash
# Plain
http GET :3000/plain/plus x==1 y==20
http POST :3000/plain/plus x:=1 y:=20
# Schema # Schema
http GET :3000/schema/plus x==1 y==20 http GET :3000/schema/plus x==1 y==20
http POST :3000/schema/plus x:=1 y:=20 http POST :3000/schema/plus x:=1 y:=20

View file

@ -2,5 +2,5 @@
:description "Reitit Ring App" :description "Reitit Ring App"
:dependencies [[org.clojure/clojure "1.9.0"] :dependencies [[org.clojure/clojure "1.9.0"]
[ring "1.6.3"] [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})

View file

@ -3,8 +3,7 @@
(def routes (def routes
["/dspec" {:coercion reitit.coercion.spec/coercion} ["/dspec" {:coercion reitit.coercion.spec/coercion}
["/plus" {:name ::plus ["/plus" {:responses {200 {:body {:total int?}}}
:responses {200 {:body {:total int?}}}
:get {:summary "plus with query-params" :get {:summary "plus with query-params"
:parameters {:query {:x int?, :y int?}} :parameters {:query {:x int?, :y int?}}
:handler (fn [{{{:keys [x y]} :query} :parameters}] :handler (fn [{{{:keys [x y]} :query} :parameters}]

View file

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

View file

@ -4,8 +4,7 @@
(def routes (def routes
["/schema" {:coercion reitit.coercion.schema/coercion} ["/schema" {:coercion reitit.coercion.schema/coercion}
["/plus" {:name ::plus ["/plus" {:responses {200 {:body {:total s/Int}}}
:responses {200 {:body {:total s/Int}}}
:get {:summary "plus with query-params" :get {:summary "plus with query-params"
:parameters {:query {:x s/Int, :y s/Int}} :parameters {:query {:x s/Int, :y s/Int}}
:handler (fn [{{{:keys [x y]} :query} :parameters}] :handler (fn [{{{:keys [x y]} :query} :parameters}]

View file

@ -1,33 +1,33 @@
(ns example.server (ns example.server
(:require [ring.adapter.jetty :as jetty] (:require [ring.adapter.jetty :as jetty]
[ring.middleware.params] [ring.middleware.params :as params]
[muuntaja.middleware] [reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring :as ring] [muuntaja.core :as m]
[reitit.ring.coercion :as coercion] [reitit.ring.coercion :as coercion]
[reitit.ring :as ring]
[example.plain]
[example.dspec] [example.dspec]
[example.schema] [example.schema]
[example.spec])) [example.spec]))
(defonce ^:private server (atom nil))
(def app (def app
(ring/ring-handler (ring/ring-handler
(ring/router (ring/router
[example.schema/routes [example.plain/routes
example.schema/routes
example.dspec/routes example.dspec/routes
example.spec/routes] example.spec/routes]
{:data {:middleware [ring.middleware.params/wrap-params {:data {:muuntaja m/instance
muuntaja.middleware/wrap-format :middleware [params/wrap-params
muuntaja/format-middleware
coercion/coerce-exceptions-middleware coercion/coerce-exceptions-middleware
coercion/coerce-request-middleware coercion/coerce-request-middleware
coercion/coerce-response-middleware]}}))) coercion/coerce-response-middleware]}})
(ring/create-default-handler)))
(defn restart [] (defn start []
(swap! server (fn [x] (jetty/run-jetty #'app {:port 3000, :join? false})
(when x (.stop x))
(jetty/run-jetty
app
{:port 3000, :join? false})))
(println "server running in port 3000")) (println "server running in port 3000"))
(restart) (comment
(start))

View file

@ -10,8 +10,7 @@
(def routes (def routes
["/spec" {:coercion reitit.coercion.spec/coercion} ["/spec" {:coercion reitit.coercion.spec/coercion}
["/plus" {:name ::plus ["/plus" {:responses {200 {:body (s/keys :req-un [::total])}}
:responses {200 {:body (s/keys :req-un [::total])}}
:get {:summary "plus with query-params" :get {:summary "plus with query-params"
:parameters {:query (s/keys :req-un [::x ::y])} :parameters {:query (s/keys :req-un [::x ::y])}
:handler (fn [{{{:keys [x y]} :query} :parameters}] :handler (fn [{{{:keys [x y]} :query} :parameters}]