mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
update example
This commit is contained in:
parent
218db83267
commit
9729a7424e
7 changed files with 36 additions and 28 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -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}]
|
||||
|
|
|
|||
10
examples/ring-example/src/example/plain.clj
Normal file
10
examples/ring-example/src/example/plain.clj
Normal 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)}})}]])
|
||||
|
|
@ -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}]
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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}]
|
||||
|
|
|
|||
Loading…
Reference in a new issue