mirror of
https://github.com/metosin/reitit.git
synced 2026-02-15 15:55:15 +00:00
example copied
This commit is contained in:
parent
044c0d6163
commit
91473c5976
6 changed files with 129 additions and 0 deletions
11
examples/http-swagger/.gitignore
vendored
Normal file
11
examples/http-swagger/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/target
|
||||||
|
/classes
|
||||||
|
/checkouts
|
||||||
|
pom.xml
|
||||||
|
pom.xml.asc
|
||||||
|
*.jar
|
||||||
|
*.class
|
||||||
|
/.lein-*
|
||||||
|
/.nrepl-port
|
||||||
|
.hgignore
|
||||||
|
.hg/
|
||||||
26
examples/http-swagger/README.md
Normal file
26
examples/http-swagger/README.md
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Http with Swagger example
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```clj
|
||||||
|
> lein repl
|
||||||
|
(start)
|
||||||
|
```
|
||||||
|
|
||||||
|
To test the endpoints using [httpie](https://httpie.org/):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
http GET :3000/api/schema/plus x==1 y==20
|
||||||
|
http GET :3000/api/spec/plus x==1 y==20
|
||||||
|
|
||||||
|
http GET :3000/api/swagger.json
|
||||||
|
```
|
||||||
|
|
||||||
|
* swagger.json: http://localhost:3000/api/swagger.json
|
||||||
|
* swagger-ui: http://localhost:3000/api-docs/
|
||||||
|
|
||||||
|
<img src="https://raw.githubusercontent.com/metosin/reitit/master/examples/http-swagger/swagger-ui.png" />
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Copyright © 2017-2018 Metosin Oy
|
||||||
6
examples/http-swagger/project.clj
Normal file
6
examples/http-swagger/project.clj
Normal file
|
|
@ -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.0-SNAPSHOT"]]
|
||||||
|
:repl-options {:init-ns example.server})
|
||||||
BIN
examples/http-swagger/resources/reitit.png
Normal file
BIN
examples/http-swagger/resources/reitit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 494 KiB |
86
examples/http-swagger/src/example/server.clj
Normal file
86
examples/http-swagger/src/example/server.clj
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
(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.java.io :as io]))
|
||||||
|
|
||||||
|
(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 multipart/temp-file-part}}
|
||||||
|
:responses {200 {:body {:file multipart/temp-file-part}}}
|
||||||
|
:handler (fn [{{{:keys [file]} :multipart} :parameters}]
|
||||||
|
{:status 200
|
||||||
|
:body {:file 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 {:x int?, :y int?}}
|
||||||
|
:responses {200 {:body {:total int?}}}
|
||||||
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
||||||
|
{:status 200
|
||||||
|
:body {:total (+ x y)}})}
|
||||||
|
:post {:summary "plus with spec body parameters"
|
||||||
|
:parameters {:body {:x int?, :y int?}}
|
||||||
|
:responses {200 {:body {:total int?}}}
|
||||||
|
: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 "/"})
|
||||||
|
(ring/create-default-handler))))
|
||||||
|
|
||||||
|
(defn start []
|
||||||
|
(jetty/run-jetty #'app {:port 3000, :join? false})
|
||||||
|
(println "server running in port 3000"))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(start))
|
||||||
BIN
examples/http-swagger/swagger-ui.png
Normal file
BIN
examples/http-swagger/swagger-ui.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 203 KiB |
Loading…
Reference in a new issue