Full stack with data-specs

This commit is contained in:
Tommi Reiman 2018-07-22 16:27:20 +03:00
parent 0f384c2b52
commit 44feacd7d3

View file

@ -2,13 +2,11 @@
(:require [reitit.ring :as ring] (:require [reitit.ring :as ring]
[reitit.swagger :as swagger] [reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui] [reitit.swagger-ui :as swagger-ui]
[reitit.ring.coercion :as rrc] [reitit.ring.coercion :as coercion]
[reitit.coercion.spec :as spec] [reitit.coercion.spec]
[reitit.coercion.schema :as schema]
[reitit.ring.middleware.muuntaja :as muuntaja] [reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.exception :as exception]
[schema.core :refer [Int]] [ring.middleware.params :as params]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[ring.middleware.params] [ring.middleware.params]
[muuntaja.core :as m])) [muuntaja.core :as m]))
@ -16,16 +14,13 @@
(def app (def app
(ring/ring-handler (ring/ring-handler
(ring/router (ring/router
["/api" [["/swagger.json"
["/swagger.json"
{:get {:no-doc true {:get {:no-doc true
:swagger {:info {:title "my-api"}} :swagger {:info {:title "my-api"}}
:handler (swagger/create-swagger-handler)}}] :handler (swagger/create-swagger-handler)}}]
["/spec" ["/math"
{:coercion spec/coercion {:swagger {:tags ["math"]}}
:swagger {:tags ["spec"]}}
["/plus" ["/plus"
{:get {:summary "plus with spec query parameters" {:get {:summary "plus with spec query parameters"
@ -37,38 +32,28 @@
:post {:summary "plus with spec body parameters" :post {:summary "plus with spec body parameters"
:parameters {:body {:x int?, :y int?}} :parameters {:body {:x int?, :y int?}}
:responses {200 {:body {:total int?}}} :responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (+ x y)}})}}]]
["/schema"
{:coercion schema/coercion
:swagger {:tags ["schema"]}}
["/plus"
{:get {:summary "plus with schema 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 schema body parameters"
:parameters {:body {:x Int, :y Int}}
:responses {200 {:body {:total Int}}}
:handler (fn [{{{:keys [x y]} :body} :parameters}] :handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200 {:status 200
:body {:total (+ x y)}})}}]]] :body {:total (+ x y)}})}}]]]
{:data {:muuntaja m/instance {:data {:coercion reitit.coercion.spec/coercion
:middleware [ring.middleware.params/wrap-params :muuntaja m/instance
muuntaja/format-middleware :middleware [;; query-params & form-params
swagger/swagger-feature params/wrap-params
rrc/coerce-exceptions-middleware ;; content-negotiation
rrc/coerce-request-middleware muuntaja/format-negotiate-middleware
rrc/coerce-response-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]}})
(ring/routes (ring/routes
(swagger-ui/create-swagger-ui-handler (swagger-ui/create-swagger-ui-handler {:path "/"})
{:path "/", :url "/api/swagger.json"})
(ring/create-default-handler)))) (ring/create-default-handler))))
(defn start [] (defn start []