reitit/examples/ring-malli-swagger/src/example/server.clj

168 lines
7.9 KiB
Clojure
Raw Normal View History

2019-12-27 22:52:40 +00:00
(ns example.server
(:require [reitit.ring :as ring]
2019-12-28 00:39:47 +00:00
[reitit.coercion.malli]
[reitit.openapi :as openapi]
2019-12-28 00:39:47 +00:00
[reitit.ring.malli]
2019-12-27 22:52:40 +00:00
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[reitit.ring.coercion :as coercion]
[reitit.dev.pretty :as pretty]
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.exception :as exception]
[reitit.ring.middleware.multipart :as multipart]
[reitit.ring.middleware.parameters :as parameters]
; [reitit.ring.middleware.dev :as dev]
; [reitit.ring.spec :as spec]
; [spec-tools.spell :as spell]
[ring.adapter.jetty :as jetty]
[muuntaja.core :as m]
2020-01-08 19:50:04 +00:00
[clojure.java.io :as io]
[malli.util :as mu]))
2019-12-27 22:52:40 +00:00
(def app
(ring/ring-handler
(ring/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "my-api"
:description "swagger docs with [malli](https://github.com/metosin/malli) and reitit-ring"
:version "0.0.1"}
;; used in /secure APIs below
:securityDefinitions {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}
2020-01-10 14:25:35 +00:00
:tags [{:name "files", :description "file api"}
{:name "math", :description "math api"}]}
2019-12-27 22:52:40 +00:00
:handler (swagger/create-swagger-handler)}}]
["/openapi.json"
2023-03-17 08:12:15 +00:00
{:get {:no-doc true
:openapi {:info {:title "my-api"
:description "openapi3 docs with [malli](https://github.com/metosin/malli) and reitit-ring"
:version "0.0.1"}
;; used in /secure APIs below
:components {:securitySchemes {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}}}
2023-03-17 08:12:15 +00:00
:handler (openapi/create-openapi-handler)}}]
2019-12-27 22:52:40 +00:00
["/files"
{:tags ["files"]}
2019-12-27 22:52:40 +00:00
["/upload"
{:post {:summary "upload a file"
2019-12-28 00:39:47 +00:00
:parameters {:multipart [:map [:file reitit.ring.malli/temp-file-part]]}
:responses {200 {:body [:map [:name string?] [:size int?]]}}
2019-12-27 22:52:40 +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"]}
:responses {200 {:description "an image"
:content {"image/png" any?}}}
2019-12-27 22:52:40 +00:00
:handler (fn [_]
{:status 200
:headers {"Content-Type" "image/png"}
:body (-> "reitit.png"
(io/resource)
(io/input-stream))})}}]]
["/math"
{:tags ["math"]}
2019-12-27 22:52:40 +00:00
["/plus"
2020-02-14 14:27:45 +00:00
{:get {:summary "plus with malli query parameters"
:parameters {:query [:map
[:x
{:title "X parameter"
:description "Description for X parameter"
:json-schema/default 42}
int?]
[:y int?]]}
2019-12-28 00:39:47 +00:00
:responses {200 {:body [:map [:total int?]]}}
2019-12-27 22:52:40 +00:00
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (+ x y)}})}
2020-02-14 14:27:45 +00:00
:post {:summary "plus with malli body parameters"
:parameters {:body [:map
[:x
{:title "X parameter"
:description "Description for X parameter"
:json-schema/default 42}
int?]
[:y int?]]}
2019-12-28 00:39:47 +00:00
:responses {200 {:body [:map [:total int?]]}}
2019-12-27 22:52:40 +00:00
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (+ x y)}})}}]]
["/secure"
{:tags ["secure"]
:openapi {:security [{"auth" []}]}
:swagger {:security [{"auth" []}]}}
["/get"
{:get {:summary "endpoint authenticated with a header"
:responses {200 {:body [:map [:secret :string]]}
401 {:body [:map [:error :string]]}}
:handler (fn [request]
;; In a real app authentication would be handled by middleware
(if (= "secret" (get-in request [:headers "example-api-key"]))
{:status 200
:body {:secret "I am a marmot"}}
{:status 401
:body {:error "unauthorized"}}))}}]]]
2019-12-27 22:52:40 +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.malli/create
2020-01-08 19:50:04 +00:00
{;; set of keys to include in error messages
:error-keys #{#_:type :coercion :in :schema :value :errors :humanized #_:transformed}
;; schema identity function (default: close all map schemas)
:compile mu/closed-schema
;; strip-extra-keys (effects only predefined transformers)
:strip-extra-keys true
;; add/set default values
:default-values true
;; malli options
:options nil})
2019-12-27 22:52:40 +00:00
:muuntaja m/instance
:middleware [;; swagger & openapi
2019-12-27 22:52:40 +00:00
swagger/swagger-feature
openapi/openapi-feature
2019-12-27 22:52:40 +00:00
;; query-params & form-params
parameters/parameters-middleware
;; 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 "/"
:config {:validatorUrl nil
:urls [{:name "swagger", :url "swagger.json"}
{:name "openapi", :url "openapi.json"}]
:urls.primaryName "openapi"
2019-12-27 22:52:40 +00:00
:operationsSorter "alpha"}})
(ring/create-default-handler))))
(defn start []
(jetty/run-jetty #'app {:port 3000, :join? false})
(println "server running in port 3000"))
(comment
(start))