mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
initial commit
This commit is contained in:
parent
0a872d8f3a
commit
0ac3a14805
7 changed files with 209 additions and 0 deletions
11
examples/ring-malli-lite-swagger/.gitignore
vendored
Normal file
11
examples/ring-malli-lite-swagger/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/target
|
||||
/classes
|
||||
/checkouts
|
||||
pom.xml
|
||||
pom.xml.asc
|
||||
*.jar
|
||||
*.class
|
||||
/.lein-*
|
||||
/.nrepl-port
|
||||
.hgignore
|
||||
.hg/
|
||||
23
examples/ring-malli-lite-swagger/README.md
Normal file
23
examples/ring-malli-lite-swagger/README.md
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# reitit-ring, malli, swagger
|
||||
|
||||
## Usage
|
||||
|
||||
```clj
|
||||
> lein repl
|
||||
(start)
|
||||
```
|
||||
|
||||
To test the endpoints using [httpie](https://httpie.org/):
|
||||
|
||||
```bash
|
||||
http GET :3000/math/plus x==1 y==20
|
||||
http POST :3000/math/plus x:=1 y:=20
|
||||
|
||||
http GET :3000/swagger.json
|
||||
```
|
||||
|
||||
<img src="https://raw.githubusercontent.com/metosin/reitit/master/examples/ring-spec-swagger/swagger.png" />
|
||||
|
||||
## License
|
||||
|
||||
Copyright © 2017-2019 Metosin Oy
|
||||
8
examples/ring-malli-lite-swagger/project.clj
Normal file
8
examples/ring-malli-lite-swagger/project.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defproject ring-example "0.1.0-SNAPSHOT"
|
||||
:description "Reitit Ring App with Swagger"
|
||||
:dependencies [[org.clojure/clojure "1.10.0"]
|
||||
[metosin/jsonista "0.2.6"]
|
||||
[ring/ring-jetty-adapter "1.7.1"]
|
||||
[metosin/reitit "0.5.16-SNAPSHOT"]]
|
||||
:repl-options {:init-ns example.server}
|
||||
:profiles {:dev {:dependencies [[ring/ring-mock "0.3.2"]]}})
|
||||
BIN
examples/ring-malli-lite-swagger/resources/reitit.png
Normal file
BIN
examples/ring-malli-lite-swagger/resources/reitit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 494 KiB |
129
examples/ring-malli-lite-swagger/src/example/server.clj
Normal file
129
examples/ring-malli-lite-swagger/src/example/server.clj
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
(ns example.server
|
||||
(:require [reitit.ring :as ring]
|
||||
[reitit.coercion.malli]
|
||||
[reitit.ring.malli]
|
||||
[reitit.swagger :as swagger]
|
||||
[reitit.swagger-ui :as swagger-ui]
|
||||
[reitit.ring.coercion :as coercion]
|
||||
[reitit.dev.pretty :as pretty]
|
||||
[reitit.ring.middleware.muuntaja :as muuntaja]
|
||||
[reitit.ring.middleware.exception :as exception]
|
||||
[reitit.ring.middleware.multipart :as multipart]
|
||||
[reitit.ring.middleware.parameters :as parameters]
|
||||
; [reitit.ring.middleware.dev :as dev]
|
||||
; [reitit.ring.spec :as spec]
|
||||
; [spec-tools.spell :as spell]
|
||||
[ring.adapter.jetty :as jetty]
|
||||
[muuntaja.core :as m]
|
||||
[clojure.java.io :as io]
|
||||
[malli.util :as mu]))
|
||||
|
||||
(def app
|
||||
(ring/ring-handler
|
||||
(ring/router
|
||||
[["/swagger.json"
|
||||
{:get {:no-doc true
|
||||
:swagger {:info {:title "my-api"
|
||||
:description "with [malli](https://github.com/metosin/malli) and reitit-ring"}
|
||||
:tags [{:name "files", :description "file api"}
|
||||
{:name "math", :description "math api"}]}
|
||||
:handler (swagger/create-swagger-handler)}}]
|
||||
|
||||
["/files"
|
||||
{:swagger {:tags ["files"]}}
|
||||
|
||||
["/upload"
|
||||
{:post {:summary "upload a file"
|
||||
:parameters {:multipart [:map [:file reitit.ring.malli/temp-file-part]]}
|
||||
:responses {200 {:body [:map [:name string?] [:size int?]]}}
|
||||
:handler (fn [{{{:keys [file]} :multipart} :parameters}]
|
||||
{:status 200
|
||||
:body {:name (:filename file)
|
||||
:size (:size file)}})}}]
|
||||
|
||||
["/download"
|
||||
{:get {:summary "downloads a file"
|
||||
:swagger {:produces ["image/png"]}
|
||||
:handler (fn [_]
|
||||
{:status 200
|
||||
:headers {"Content-Type" "image/png"}
|
||||
:body (-> "reitit.png"
|
||||
(io/resource)
|
||||
(io/input-stream))})}}]]
|
||||
|
||||
["/math"
|
||||
{:swagger {:tags ["math"]}}
|
||||
|
||||
["/plus"
|
||||
{:get {:summary "plus with malli query parameters"
|
||||
:parameters {:query [:map
|
||||
[:x
|
||||
{:title "X parameter"
|
||||
:description "Description for X parameter"
|
||||
:json-schema/default 42}
|
||||
int?]
|
||||
[:y int?]]}
|
||||
:responses {200 {:body [:map [:total int?]]}}
|
||||
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||
{:status 200
|
||||
:body {:total (+ x y)}})}
|
||||
:post {:summary "plus with malli body parameters"
|
||||
:parameters {:body [:map
|
||||
[:x
|
||||
{:title "X parameter"
|
||||
:description "Description for X parameter"
|
||||
:json-schema/default 42}
|
||||
int?]
|
||||
[:y int?]]}
|
||||
:responses {200 {:body [:map [:total int?]]}}
|
||||
:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
||||
{:status 200
|
||||
:body {:total (+ x y)}})}}]]]
|
||||
|
||||
{;;:reitit.middleware/transform dev/print-request-diffs ;; pretty diffs
|
||||
;;:validate spec/validate ;; enable spec validation for route data
|
||||
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
|
||||
:exception pretty/exception
|
||||
:data {:coercion (reitit.coercion.malli/create
|
||||
{;; set of keys to include in error messages
|
||||
:error-keys #{#_:type :coercion :in :schema :value :errors :humanized #_:transformed}
|
||||
;; schema identity function (default: close all map schemas)
|
||||
:compile mu/closed-schema
|
||||
;; strip-extra-keys (effects only predefined transformers)
|
||||
:strip-extra-keys true
|
||||
;; add/set default values
|
||||
:default-values true
|
||||
;; malli options
|
||||
:options nil})
|
||||
:muuntaja m/instance
|
||||
:middleware [;; swagger feature
|
||||
swagger/swagger-feature
|
||||
;; query-params & form-params
|
||||
parameters/parameters-middleware
|
||||
;; content-negotiation
|
||||
muuntaja/format-negotiate-middleware
|
||||
;; encoding response body
|
||||
muuntaja/format-response-middleware
|
||||
;; exception handling
|
||||
exception/exception-middleware
|
||||
;; decoding request body
|
||||
muuntaja/format-request-middleware
|
||||
;; coercing response bodys
|
||||
coercion/coerce-response-middleware
|
||||
;; coercing request parameters
|
||||
coercion/coerce-request-middleware
|
||||
;; multipart
|
||||
multipart/multipart-middleware]}})
|
||||
(ring/routes
|
||||
(swagger-ui/create-swagger-ui-handler
|
||||
{:path "/"
|
||||
:config {:validatorUrl nil
|
||||
:operationsSorter "alpha"}})
|
||||
(ring/create-default-handler))))
|
||||
|
||||
(defn start []
|
||||
(jetty/run-jetty #'app {:port 3000, :join? false})
|
||||
(println "server running in port 3000"))
|
||||
|
||||
(comment
|
||||
(start))
|
||||
BIN
examples/ring-malli-lite-swagger/swagger.png
Normal file
BIN
examples/ring-malli-lite-swagger/swagger.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 228 KiB |
|
|
@ -0,0 +1,38 @@
|
|||
(ns example.server-test
|
||||
(:require [clojure.test :refer [deftest testing is]]
|
||||
[example.server :refer [app]]
|
||||
[ring.mock.request :refer [request json-body]]))
|
||||
|
||||
(deftest example-server
|
||||
|
||||
(testing "GET"
|
||||
(is (= (-> (request :get "/math/plus?x=20&y=3")
|
||||
app :body slurp)
|
||||
(-> {:request-method :get :uri "/math/plus" :query-string "x=20&y=3"}
|
||||
app :body slurp)
|
||||
(-> {:request-method :get :uri "/math/plus" :query-params {:x 20 :y 3}}
|
||||
app :body slurp)
|
||||
"{\"total\":23}")))
|
||||
|
||||
(testing "POST"
|
||||
(is (= (-> (request :post "/math/plus") (json-body {:x 40 :y 2})
|
||||
app :body slurp)
|
||||
(-> {:request-method :post :uri "/math/plus" :body-params {:x 40 :y 2}}
|
||||
app :body slurp)
|
||||
"{\"total\":42}")))
|
||||
|
||||
(testing "Download"
|
||||
(is (= (-> {:request-method :get :uri "/files/download"}
|
||||
app :body (#(slurp % :encoding "ascii")) count) ;; binary
|
||||
(.length (clojure.java.io/file "resources/reitit.png"))
|
||||
506325)))
|
||||
|
||||
(testing "Upload"
|
||||
(let [file (clojure.java.io/file "resources/reitit.png")
|
||||
multipart-temp-file-part {:tempfile file
|
||||
:size (.length file)
|
||||
:filename (.getName file)
|
||||
:content-type "image/png;"}]
|
||||
(is (= (-> {:request-method :post :uri "/files/upload" :multipart-params {:file multipart-temp-file-part}}
|
||||
app :body slurp)
|
||||
"{\"name\":\"reitit.png\",\"size\":506325}")))))
|
||||
Loading…
Reference in a new issue