mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 17:01:11 +00:00
ring-example
This commit is contained in:
parent
59aa364932
commit
e0eb1fd0a3
8 changed files with 149 additions and 0 deletions
11
examples/ring-example/.gitignore
vendored
Normal file
11
examples/ring-example/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/target
|
||||||
|
/classes
|
||||||
|
/checkouts
|
||||||
|
pom.xml
|
||||||
|
pom.xml.asc
|
||||||
|
*.jar
|
||||||
|
*.class
|
||||||
|
/.lein-*
|
||||||
|
/.nrepl-port
|
||||||
|
.hgignore
|
||||||
|
.hg/
|
||||||
33
examples/ring-example/README.md
Normal file
33
examples/ring-example/README.md
Normal file
|
|
@ -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
|
||||||
6
examples/ring-example/project.clj
Normal file
6
examples/ring-example/project.clj
Normal file
|
|
@ -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"]])
|
||||||
19
examples/ring-example/src/example/dspec.clj
Normal file
19
examples/ring-example/src/example/dspec.clj
Normal file
|
|
@ -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)}})}}]])
|
||||||
20
examples/ring-example/src/example/schema.clj
Normal file
20
examples/ring-example/src/example/schema.clj
Normal file
|
|
@ -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)}})}}]])
|
||||||
33
examples/ring-example/src/example/server.clj
Normal file
33
examples/ring-example/src/example/server.clj
Normal file
|
|
@ -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)
|
||||||
26
examples/ring-example/src/example/spec.clj
Normal file
26
examples/ring-example/src/example/spec.clj
Normal file
|
|
@ -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)}})}}]])
|
||||||
|
|
@ -45,6 +45,7 @@
|
||||||
[expound "0.3.2"]
|
[expound "0.3.2"]
|
||||||
[orchestra "2017.08.13"]
|
[orchestra "2017.08.13"]
|
||||||
|
|
||||||
|
[ring "1.6.3"]
|
||||||
[metosin/muuntaja "0.4.1"]
|
[metosin/muuntaja "0.4.1"]
|
||||||
[metosin/jsonista "0.1.0-SNAPSHOT"]
|
[metosin/jsonista "0.1.0-SNAPSHOT"]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue