2018-04-19 19:58:59 +00:00
|
|
|
(ns example.server
|
|
|
|
|
(:require [reitit.ring :as ring]
|
|
|
|
|
[reitit.swagger :as swagger]
|
2018-05-14 05:21:47 +00:00
|
|
|
[reitit.swagger-ui :as swagger-ui]
|
2018-04-19 19:58:59 +00:00
|
|
|
[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
|
2018-05-07 06:03:40 +00:00
|
|
|
["/api"
|
|
|
|
|
{:swagger {:id ::math}}
|
2018-04-19 19:58:59 +00:00
|
|
|
|
2018-05-07 06:03:40 +00:00
|
|
|
["/swagger.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:swagger {:info {:title "my-api"}}
|
|
|
|
|
:handler (swagger/create-swagger-handler)}}]
|
2018-04-19 19:58:59 +00:00
|
|
|
|
2018-05-07 06:03:40 +00:00
|
|
|
["/spec"
|
|
|
|
|
{:coercion spec/coercion
|
|
|
|
|
:swagger {:tags ["spec"]}}
|
2018-05-07 05:59:21 +00:00
|
|
|
|
2018-05-07 06:03:40 +00:00
|
|
|
["/plus"
|
2018-06-10 20:14:11 +00:00
|
|
|
{:get {:summary "plus with spec query parameters"
|
2018-05-07 06:03:40 +00:00
|
|
|
:parameters {:query {:x int?, :y int?}}
|
|
|
|
|
:responses {200 {:body {:total int?}}}
|
|
|
|
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
|
|
|
|
{:status 200
|
2018-06-10 20:14:11 +00:00
|
|
|
: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)}})}}]]
|
2018-05-07 05:59:21 +00:00
|
|
|
|
2018-05-07 06:03:40 +00:00
|
|
|
["/schema"
|
|
|
|
|
{:coercion schema/coercion
|
|
|
|
|
:swagger {:tags ["schema"]}}
|
2018-04-19 19:58:59 +00:00
|
|
|
|
2018-05-07 06:03:40 +00:00
|
|
|
["/plus"
|
2018-06-10 20:14:11 +00:00
|
|
|
{:get {:summary "plus with schema query parameters"
|
2018-05-07 06:03:40 +00:00
|
|
|
:parameters {:query {:x Int, :y Int}}
|
|
|
|
|
:responses {200 {:body {:total Int}}}
|
|
|
|
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
|
|
|
|
{:status 200
|
2018-06-10 20:14:11 +00:00
|
|
|
:body {:total (+ x y)}})}
|
|
|
|
|
:post {:summary "plus with schema 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)}})}}]]]
|
2018-04-19 19:58:59 +00:00
|
|
|
|
|
|
|
|
{:data {:middleware [ring.middleware.params/wrap-params
|
|
|
|
|
muuntaja.middleware/wrap-format
|
|
|
|
|
swagger/swagger-feature
|
|
|
|
|
rrc/coerce-exceptions-middleware
|
|
|
|
|
rrc/coerce-request-middleware
|
2018-05-14 05:21:47 +00:00
|
|
|
rrc/coerce-response-middleware]
|
|
|
|
|
:swagger {:produces #{"application/json"
|
|
|
|
|
"application/edn"
|
|
|
|
|
"application/transit+json"}
|
|
|
|
|
:consumes #{"application/json"
|
|
|
|
|
"application/edn"
|
2018-06-10 20:14:11 +00:00
|
|
|
"application/transit+json"}}}
|
|
|
|
|
;; TODO: these should work by default!
|
|
|
|
|
:extract-request-format (comp :format :muuntaja/request)
|
|
|
|
|
:extract-response-format (comp :format :muuntaja/response)})
|
2018-04-29 12:18:30 +00:00
|
|
|
(ring/routes
|
2018-05-14 05:21:47 +00:00
|
|
|
(swagger-ui/create-swagger-ui-handler
|
2018-05-31 13:46:56 +00:00
|
|
|
{:path "/", :url "/api/swagger.json"})
|
2018-04-29 12:18:30 +00:00
|
|
|
(ring/create-default-handler))))
|
2018-04-19 19:58:59 +00:00
|
|
|
|
|
|
|
|
(defn start []
|
|
|
|
|
(jetty/run-jetty #'app {:port 3000, :join? false})
|
|
|
|
|
(println "server running in port 3000"))
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
|
(start))
|