reitit/examples/http-swagger/src/example/server.clj

192 lines
8.5 KiB
Clojure
Raw Normal View History

2018-09-07 20:11:59 +00:00
(ns example.server
(:require [reitit.ring :as ring]
[reitit.http :as http]
[reitit.coercion.spec]
2018-09-07 20:11:59 +00:00
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
2023-03-07 08:34:28 +00:00
[reitit.openapi :as openapi]
2018-09-07 20:11:59 +00:00
[reitit.http.coercion :as coercion]
[reitit.dev.pretty :as pretty]
[reitit.interceptor.sieppari :as sieppari]
2018-09-07 20:11:59 +00:00
[reitit.http.interceptors.parameters :as parameters]
[reitit.http.interceptors.muuntaja :as muuntaja]
[reitit.http.interceptors.exception :as exception]
[reitit.http.interceptors.multipart :as multipart]
;; Uncomment to use
; [reitit.http.interceptors.dev :as dev]
; [reitit.http.spec :as spec]
; [spec-tools.spell :as spell]
2018-09-07 20:11:59 +00:00
[ring.adapter.jetty :as jetty]
2019-08-30 10:11:08 +00:00
[aleph.http :as aleph]
2018-09-07 20:11:59 +00:00
[muuntaja.core :as m]
2018-10-21 20:11:09 +00:00
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[spec-tools.core :as st]
2020-05-16 14:53:43 +00:00
[sieppari.async.manifold]
2018-10-21 20:11:09 +00:00
[manifold.deferred :as d]))
(s/def ::x int?)
(s/def ::y int?)
(s/def ::total pos-int?)
(s/def ::seed string?)
(s/def ::results
(st/spec
{:spec (s/and int? #(< 0 % 100))
:description "between 1-100"
:swagger/default 10
:reason "invalid number"}))
2018-09-07 20:11:59 +00:00
(def app
(http/ring-handler
(http/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "my-api"
2023-03-14 19:09:03 +00:00
:description "swagger-docs with reitit-http"
:version "0.0.1"}
;; used in /secure APIs below
:securityDefinitions {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}}
2018-09-07 20:11:59 +00:00
:handler (swagger/create-swagger-handler)}}]
2023-03-07 08:34:28 +00:00
["/openapi.json"
{:get {:no-doc true
:openapi {:info {:title "my-api"
2023-03-15 11:20:02 +00:00
:description "openapi3-docs with reitit-http"
:version "0.0.1"}
;; used in /secure APIs below
:components {:securitySchemes {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}}}
2023-03-07 08:34:28 +00:00
:handler (openapi/create-openapi-handler)}}]
2018-09-07 20:11:59 +00:00
["/files"
{:tags ["files"]}
2018-09-07 20:11:59 +00:00
["/upload"
{:post {:summary "upload a file"
:parameters {:multipart {:file multipart/temp-file-part}}
:responses {200 {:body {:name string?, :size int?}}}
: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"))})}}]]
2018-10-21 20:11:09 +00:00
["/async"
{:get {:tags ["async"]
2018-10-21 20:11:09 +00:00
:summary "fetches random users asynchronously over the internet"
:parameters {:query (s/keys :req-un [::results] :opt-un [::seed])}
:responses {200 {:body any?}}
:handler (fn [{{{:keys [seed results]} :query} :parameters}]
(d/chain
2019-08-30 10:11:08 +00:00
(aleph/get
2018-10-21 20:11:09 +00:00
"https://randomuser.me/api/"
{:query-params {:seed seed, :results results}})
:body
(partial m/decode "application/json")
2018-10-21 20:11:09 +00:00
:results
(fn [results]
{:status 200
:body results})))}}]
2018-09-07 20:11:59 +00:00
["/math"
{:tags ["math"]}
2018-09-07 20:11:59 +00:00
["/plus"
2018-10-21 20:11:09 +00:00
{:get {:summary "plus with data-spec query parameters"
2018-09-07 20:11:59 +00:00
:parameters {:query {:x int?, :y int?}}
2018-10-21 20:11:09 +00:00
:responses {200 {:body {:total pos-int?}}}
2018-09-07 20:11:59 +00:00
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (+ x y)}})}
2018-10-21 20:11:09 +00:00
:post {:summary "plus with data-spec body parameters"
2018-09-07 20:11:59 +00:00
:parameters {:body {:x int?, :y int?}}
:responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
2018-10-21 20:11:09 +00:00
:body {:total (+ x y)}})}}]
["/minus"
{:get {:summary "minus with clojure.spec query parameters"
:parameters {:query (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (- x y)}})}
:post {:summary "minus with clojure.spec body parameters"
:parameters {:body (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
: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 {:secret string?}}
401 {:body {: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"}}))}}]]]
2018-09-07 20:11:59 +00:00
{;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
2019-05-11 07:25:19 +00:00
;;:validate spec/validate ;; enable spec validation for route data
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
2019-04-20 02:25:59 +00:00
:exception pretty/exception
:data {:coercion reitit.coercion.spec/coercion
2018-09-07 20:11:59 +00:00
:muuntaja m/instance
2019-04-20 02:25:59 +00:00
:interceptors [;; swagger feature
swagger/swagger-feature
;; openapi feature
openapi/openapi-feature
2019-04-20 02:25:59 +00:00
;; query-params & form-params
2018-09-07 20:11:59 +00:00
(parameters/parameters-interceptor)
;; content-negotiation
(muuntaja/format-negotiate-interceptor)
;; encoding response body
(muuntaja/format-response-interceptor)
;; exception handling
(exception/exception-interceptor)
;; decoding request body
(muuntaja/format-request-interceptor)
;; coercing response bodys
(coercion/coerce-response-interceptor)
;; coercing request parameters
(coercion/coerce-request-interceptor)
;; multipart
(multipart/multipart-interceptor)]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/"
2019-05-11 11:27:08 +00:00
:config {:validatorUrl nil
2023-03-15 11:20:02 +00:00
:urls [{:name "swagger", :url "swagger.json"}
{:name "openapi", :url "openapi.json"}]
:urls.primaryName "openapi"
2019-05-11 11:27:08 +00:00
:operationsSorter "alpha"}})
2018-09-07 20:11:59 +00:00
(ring/create-default-handler))
{:executor sieppari/executor}))
(defn start []
2018-10-21 20:11:09 +00:00
(jetty/run-jetty #'app {:port 3000, :join? false, :async true})
2019-08-30 10:11:22 +00:00
;(aleph/start-server (aleph/wrap-ring-async-handler #'app) {:port 3000})
2018-09-07 20:11:59 +00:00
(println "server running in port 3000"))
(comment
(start))