initial example app

This commit is contained in:
Tommi Reiman 2018-04-19 22:58:59 +03:00
parent 8432130425
commit 2487364c9f
4 changed files with 103 additions and 0 deletions

11
examples/ring-swagger/.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/

View file

@ -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

View file

@ -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})

View file

@ -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))