diff --git a/examples/ring-swagger/.gitignore b/examples/ring-swagger/.gitignore new file mode 100644 index 00000000..c53038ec --- /dev/null +++ b/examples/ring-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/ring-swagger/README.md b/examples/ring-swagger/README.md new file mode 100644 index 00000000..8b9cc8c9 --- /dev/null +++ b/examples/ring-swagger/README.md @@ -0,0 +1,28 @@ +# WIP: Ring + Swagger example + +TODO: + +* Serve Swagger-ui. + +## Usage + +```clj +> lein repl + +(require '[example.server :as server]) + +(server/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 +``` + +## License + +Copyright © 2017 Metosin Oy diff --git a/examples/ring-swagger/project.clj b/examples/ring-swagger/project.clj new file mode 100644 index 00000000..f3133c0c --- /dev/null +++ b/examples/ring-swagger/project.clj @@ -0,0 +1,7 @@ +(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/muuntaja "0.5.0"] + [metosin/reitit "0.1.1-SNAPSHOT"]] + :repl-options {:init-ns example.server}) diff --git a/examples/ring-swagger/src/example/server.clj b/examples/ring-swagger/src/example/server.clj new file mode 100644 index 00000000..6d74362d --- /dev/null +++ b/examples/ring-swagger/src/example/server.clj @@ -0,0 +1,57 @@ +(ns example.server + (:require [reitit.ring :as ring] + [reitit.swagger :as swagger] + [reitit.ring.coercion :as rrc] + [reitit.coercion.spec :as spec] + [reitit.coercion.schema :as schema] + [schema.core :refer [Int]] + + [ring.adapter.jetty :as jetty] + [ring.middleware.params] + [muuntaja.middleware])) + +(def app + (ring/ring-handler + (ring/router + [["/api" + {:swagger {:id ::math}} + + ["/swagger.json" + {:get {:no-doc true + :swagger {:info {:title "my-api"}} + :handler swagger/swagger-spec-handler}}] + + ["/spec" {:coercion spec/coercion} + ["/plus" + {:get {:summary "plus" + :parameters {:query {:x int?, :y int?}} + :responses {200 {:body {:total int?}}} + :handler (fn [{{{:keys [x y]} :query} :parameters}] + {:status 200, :body {:total (+ x y)}})}}]] + + ["/schema" {:coercion schema/coercion} + ["/plus" + {:get {:summary "plus" + :parameters {:query {:x Int, :y Int}} + :responses {200 {:body {:total Int}}} + :handler (fn [{{{:keys [x y]} :query} :parameters}] + {:status 200, :body {:total (+ x y)}})}}]]] + + ;; serve api-docs here + ["/api-docs/*" + (constantly {:status 200, :body "api-docs"})]] + + {:data {:middleware [ring.middleware.params/wrap-params + muuntaja.middleware/wrap-format + swagger/swagger-feature + rrc/coerce-exceptions-middleware + rrc/coerce-request-middleware + rrc/coerce-response-middleware]}}) + (ring/create-default-handler))) + +(defn start [] + (jetty/run-jetty #'app {:port 3000, :join? false}) + (println "server running in port 3000")) + +(comment + (start))