diff --git a/examples/http-swagger/.gitignore b/examples/http-swagger/.gitignore new file mode 100644 index 00000000..c53038ec --- /dev/null +++ b/examples/http-swagger/.gitignore @@ -0,0 +1,11 @@ +/target +/classes +/checkouts +pom.xml +pom.xml.asc +*.jar +*.class +/.lein-* +/.nrepl-port +.hgignore +.hg/ diff --git a/examples/http-swagger/README.md b/examples/http-swagger/README.md new file mode 100644 index 00000000..3fc6d580 --- /dev/null +++ b/examples/http-swagger/README.md @@ -0,0 +1,23 @@ +# Http with Swagger example + +## 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 +``` + + + +## License + +Copyright © 2018 Metosin Oy diff --git a/examples/http-swagger/project.clj b/examples/http-swagger/project.clj new file mode 100644 index 00000000..feaab57f --- /dev/null +++ b/examples/http-swagger/project.clj @@ -0,0 +1,6 @@ +(defproject ring-example "0.1.0-SNAPSHOT" + :description "Reitit Http 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}) diff --git a/examples/http-swagger/resources/reitit.png b/examples/http-swagger/resources/reitit.png new file mode 100644 index 00000000..c89c3654 Binary files /dev/null and b/examples/http-swagger/resources/reitit.png differ diff --git a/examples/http-swagger/src/example/server.clj b/examples/http-swagger/src/example/server.clj new file mode 100644 index 00000000..3d97dc3d --- /dev/null +++ b/examples/http-swagger/src/example/server.clj @@ -0,0 +1,94 @@ +(ns example.server + (:require [reitit.ring :as ring] + [reitit.http :as http] + [reitit.swagger :as swagger] + [reitit.swagger-ui :as swagger-ui] + [reitit.http.coercion :as coercion] + [reitit.coercion.spec :as spec-coercion] + [reitit.http.interceptors.parameters :as parameters] + [reitit.http.interceptors.muuntaja :as muuntaja] + [reitit.http.interceptors.exception :as exception] + [reitit.http.interceptors.multipart :as multipart] + [reitit.interceptor.sieppari :as sieppari] + [ring.adapter.jetty :as jetty] + [muuntaja.core :as m] + [clojure.java.io :as io])) + +(def app + (http/ring-handler + (http/router + [["/swagger.json" + {:get {:no-doc true + :swagger {:info {:title "my-api" + :description "with reitit-http"}} + :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 {: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 (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 spec-coercion/coercion + :muuntaja m/instance + :interceptors [;; query-params & form-params + (parameters/parameters-interceptor) + ;; content-negotiation + (muuntaja/format-negotiate-interceptor) + ;; encoding response body + (muuntaja/format-response-interceptor) + ;; exception handling + (exception/exception-interceptor) + ;; decoding request body + (muuntaja/format-request-interceptor) + ;; coercing response bodys + (coercion/coerce-response-interceptor) + ;; coercing request parameters + (coercion/coerce-request-interceptor) + ;; multipart + (multipart/multipart-interceptor)]}}) + (ring/routes + (swagger-ui/create-swagger-ui-handler + {:path "/" + :config {:validatorUrl nil}}) + (ring/create-default-handler)) + {:executor sieppari/executor})) + +(defn start [] + (jetty/run-jetty #'app {:port 3000, :join? false}) + (println "server running in port 3000")) + +(comment + (start)) diff --git a/examples/http-swagger/swagger.png b/examples/http-swagger/swagger.png new file mode 100644 index 00000000..9d5a55b8 Binary files /dev/null and b/examples/http-swagger/swagger.png differ