forked from metosin/reitit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request metosin#140 from metosin/spec-ring-swagger
Spec ring swagger
- Loading branch information
Showing
7 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/target | ||
/classes | ||
/checkouts | ||
pom.xml | ||
pom.xml.asc | ||
*.jar | ||
*.class | ||
/.lein-* | ||
/.nrepl-port | ||
.hgignore | ||
.hg/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# reitit-ring, clojure.spec, 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/spec/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-2018 Metosin Oy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(defproject ring-example "0.1.0-SNAPSHOT" | ||
:description "Reitit Ring App with Swagger" | ||
:dependencies [[org.clojure/clojure "1.9.0"] | ||
[ring "1.6.3"] | ||
[metosin/reitit "0.2.1"]] | ||
:repl-options {:init-ns example.server}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
(ns example.server | ||
(:require [reitit.ring :as ring] | ||
[reitit.swagger :as swagger] | ||
[reitit.swagger-ui :as swagger-ui] | ||
[reitit.ring.coercion :as coercion] | ||
[reitit.coercion.spec] | ||
[reitit.ring.middleware.muuntaja :as muuntaja] | ||
[reitit.ring.middleware.exception :as exception] | ||
[reitit.ring.middleware.multipart :as multipart] | ||
[ring.middleware.params :as params] | ||
[ring.adapter.jetty :as jetty] | ||
[muuntaja.core :as m] | ||
[clojure.spec.alpha :as s] | ||
[spec-tools.spec :as spec] | ||
[clojure.java.io :as io])) | ||
|
||
(s/def ::file multipart/temp-file-part) | ||
(s/def ::file-params (s/keys :req-un [::file])) | ||
|
||
(s/def ::name spec/string?) | ||
(s/def ::size spec/int?) | ||
(s/def ::file-response (s/keys :req-un [::name ::size])) | ||
|
||
(s/def ::x spec/int?) | ||
(s/def ::y spec/int?) | ||
(s/def ::total spec/int?) | ||
(s/def ::math-request (s/keys :req-un [::x ::y])) | ||
(s/def ::math-response (s/keys :req-un [::total])) | ||
|
||
(def app | ||
(ring/ring-handler | ||
(ring/router | ||
[["/swagger.json" | ||
{:get {:no-doc true | ||
:swagger {:info {:title "my-api"}} | ||
:handler (swagger/create-swagger-handler)}}] | ||
|
||
["/files" | ||
{:swagger {:tags ["files"]}} | ||
|
||
["/upload" | ||
{:post {:summary "upload a file" | ||
:parameters {:multipart ::file-params} | ||
:responses {200 {:body ::file-response}} | ||
: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 (io/input-stream | ||
(io/resource "reitit.png"))})}}]] | ||
|
||
["/math" | ||
{:swagger {:tags ["math"]}} | ||
|
||
["/plus" | ||
{:get {:summary "plus with spec query parameters" | ||
:parameters {:query ::math-request} | ||
:responses {200 {:body ::math-response}} | ||
:handler (fn [{{{:keys [x y]} :query} :parameters}] | ||
{:status 200 | ||
:body {:total (+ x y)}})} | ||
:post {:summary "plus with spec body parameters" | ||
:parameters {:body ::math-request} | ||
:responses {200 {:body ::math-response}} | ||
:handler (fn [{{{:keys [x y]} :body} :parameters}] | ||
{:status 200 | ||
:body {:total (+ x y)}})}}]]] | ||
|
||
{:data {:coercion reitit.coercion.spec/coercion | ||
:muuntaja m/instance | ||
:middleware [;; query-params & form-params | ||
params/wrap-params | ||
;; 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}}) | ||
(ring/create-default-handler)))) | ||
|
||
(defn start [] | ||
(jetty/run-jetty #'app {:port 3000, :join? false}) | ||
(println "server running in port 3000")) | ||
|
||
(comment | ||
(start)) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters