2018-09-05 10:14:58 +00:00
|
|
|
(ns example.server
|
|
|
|
|
(:require [reitit.ring :as ring]
|
2019-05-11 11:25:25 +00:00
|
|
|
[reitit.coercion.spec]
|
2018-09-05 10:14:58 +00:00
|
|
|
[reitit.swagger :as swagger]
|
|
|
|
|
[reitit.swagger-ui :as swagger-ui]
|
|
|
|
|
[reitit.ring.coercion :as coercion]
|
2019-05-11 11:25:25 +00:00
|
|
|
[reitit.dev.pretty :as pretty]
|
2018-09-05 10:14:58 +00:00
|
|
|
[reitit.ring.middleware.muuntaja :as muuntaja]
|
|
|
|
|
[reitit.ring.middleware.exception :as exception]
|
|
|
|
|
[reitit.ring.middleware.multipart :as multipart]
|
2018-09-07 20:39:55 +00:00
|
|
|
[reitit.ring.middleware.parameters :as parameters]
|
2020-03-05 11:32:48 +00:00
|
|
|
;; Uncomment to use
|
|
|
|
|
; [reitit.ring.middleware.dev :as dev]
|
|
|
|
|
; [reitit.ring.spec :as spec]
|
|
|
|
|
; [spec-tools.spell :as spell]
|
2021-03-11 19:22:33 +00:00
|
|
|
[spec-tools.core :as st]
|
2018-09-05 10:14:58 +00:00
|
|
|
[ring.adapter.jetty :as jetty]
|
|
|
|
|
[muuntaja.core :as m]
|
2018-09-05 10:21:20 +00:00
|
|
|
[clojure.spec.alpha :as s]
|
2018-09-05 10:14:58 +00:00
|
|
|
[clojure.java.io :as io]))
|
|
|
|
|
|
2018-09-05 10:21:20 +00:00
|
|
|
(s/def ::file multipart/temp-file-part)
|
|
|
|
|
(s/def ::file-params (s/keys :req-un [::file]))
|
|
|
|
|
|
2018-10-21 17:06:28 +00:00
|
|
|
(s/def ::name string?)
|
|
|
|
|
(s/def ::size int?)
|
2018-09-05 10:21:20 +00:00
|
|
|
(s/def ::file-response (s/keys :req-un [::name ::size]))
|
|
|
|
|
|
2021-03-11 18:55:52 +00:00
|
|
|
;; Use data-specs to provide extra JSON-Schema properties:
|
|
|
|
|
;; https://github.com/metosin/spec-tools/blob/master/docs/04_json_schema.md#annotated-specs
|
2021-03-11 19:22:33 +00:00
|
|
|
(s/def ::x (st/spec {:spec int?
|
2021-03-11 18:55:52 +00:00
|
|
|
:name "X parameter"
|
|
|
|
|
:description "Description for X parameter"
|
|
|
|
|
:json-schema/default 42}))
|
2018-10-21 17:06:28 +00:00
|
|
|
(s/def ::y int?)
|
|
|
|
|
(s/def ::total int?)
|
2018-09-05 10:21:20 +00:00
|
|
|
(s/def ::math-request (s/keys :req-un [::x ::y]))
|
|
|
|
|
(s/def ::math-response (s/keys :req-un [::total]))
|
|
|
|
|
|
2018-09-05 10:14:58 +00:00
|
|
|
(def app
|
|
|
|
|
(ring/ring-handler
|
|
|
|
|
(ring/router
|
|
|
|
|
[["/swagger.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:swagger {:info {:title "my-api"}}
|
|
|
|
|
:handler (swagger/create-swagger-handler)}}]
|
|
|
|
|
|
|
|
|
|
["/files"
|
|
|
|
|
{:swagger {:tags ["files"]}}
|
|
|
|
|
|
|
|
|
|
["/upload"
|
|
|
|
|
{:post {:summary "upload a file"
|
2018-09-05 10:21:20 +00:00
|
|
|
:parameters {:multipart ::file-params}
|
|
|
|
|
:responses {200 {:body ::file-response}}
|
2018-09-05 10:14:58 +00:00
|
|
|
: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"
|
2018-09-05 10:21:20 +00:00
|
|
|
:parameters {:query ::math-request}
|
|
|
|
|
:responses {200 {:body ::math-response}}
|
2018-09-05 10:14:58 +00:00
|
|
|
:handler (fn [{{{:keys [x y]} :query} :parameters}]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body {:total (+ x y)}})}
|
|
|
|
|
:post {:summary "plus with spec body parameters"
|
2018-09-05 10:21:20 +00:00
|
|
|
:parameters {:body ::math-request}
|
|
|
|
|
:responses {200 {:body ::math-response}}
|
2018-09-05 10:14:58 +00:00
|
|
|
:handler (fn [{{{:keys [x y]} :body} :parameters}]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body {:total (+ x y)}})}}]]]
|
|
|
|
|
|
2019-05-11 11:25:25 +00:00
|
|
|
{;;:reitit.middleware/transform dev/print-request-diffs ;; pretty diffs
|
|
|
|
|
;;:validate spec/validate ;; enable spec validation for route data
|
|
|
|
|
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
|
|
|
|
|
:exception pretty/exception
|
|
|
|
|
:data {:coercion reitit.coercion.spec/coercion
|
2018-09-05 10:14:58 +00:00
|
|
|
:muuntaja m/instance
|
2019-05-11 11:25:25 +00:00
|
|
|
:middleware [;; swagger feature
|
|
|
|
|
swagger/swagger-feature
|
|
|
|
|
;; query-params & form-params
|
2018-09-07 20:39:55 +00:00
|
|
|
parameters/parameters-middleware
|
2018-09-05 10:14:58 +00:00
|
|
|
;; content-negotiation
|
|
|
|
|
muuntaja/format-negotiate-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
|
|
|
|
|
;; multipart
|
|
|
|
|
multipart/multipart-middleware]}})
|
|
|
|
|
(ring/routes
|
|
|
|
|
(swagger-ui/create-swagger-ui-handler
|
|
|
|
|
{:path "/"
|
2019-05-11 11:27:08 +00:00
|
|
|
:config {:validatorUrl nil
|
|
|
|
|
:operationsSorter "alpha"}})
|
2018-09-05 10:14:58 +00:00
|
|
|
(ring/create-default-handler))))
|
|
|
|
|
|
|
|
|
|
(defn start []
|
|
|
|
|
(jetty/run-jetty #'app {:port 3000, :join? false})
|
|
|
|
|
(println "server running in port 3000"))
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
|
(start))
|