2018-09-07 21:16:59 +00:00
|
|
|
(ns example.server
|
2018-12-26 13:43:26 +00:00
|
|
|
(:require [io.pedestal.http :as server]
|
2018-09-07 21:16:59 +00:00
|
|
|
[reitit.ring :as ring]
|
|
|
|
|
[reitit.http :as http]
|
2019-05-11 11:25:25 +00:00
|
|
|
[reitit.coercion.spec]
|
2018-09-07 21:16:59 +00:00
|
|
|
[reitit.swagger :as swagger]
|
|
|
|
|
[reitit.swagger-ui :as swagger-ui]
|
|
|
|
|
[reitit.http.coercion :as coercion]
|
2019-05-23 15:53:27 +00:00
|
|
|
[reitit.dev.pretty :as pretty]
|
2018-09-07 21:16:59 +00:00
|
|
|
[reitit.http.interceptors.parameters :as parameters]
|
|
|
|
|
[reitit.http.interceptors.muuntaja :as muuntaja]
|
2019-05-11 07:25:09 +00:00
|
|
|
[reitit.http.interceptors.exception :as exception]
|
2018-09-07 21:16:59 +00:00
|
|
|
[reitit.http.interceptors.multipart :as multipart]
|
2020-03-05 11:32:48 +00:00
|
|
|
;; Uncomment to use
|
|
|
|
|
; [reitit.http.interceptors.dev :as dev]
|
|
|
|
|
; [reitit.http.spec :as spec]
|
|
|
|
|
; [spec-tools.spell :as spell]
|
2019-05-11 11:25:25 +00:00
|
|
|
[reitit.pedestal :as pedestal]
|
2018-12-26 13:43:26 +00:00
|
|
|
[clojure.core.async :as a]
|
|
|
|
|
[clojure.java.io :as io]
|
|
|
|
|
[muuntaja.core :as m]))
|
2018-09-07 21:16:59 +00:00
|
|
|
|
2018-12-26 13:43:26 +00:00
|
|
|
(defn interceptor [number]
|
|
|
|
|
{:enter (fn [ctx] (a/go (update-in ctx [:request :number] (fnil + 0) number)))})
|
|
|
|
|
|
|
|
|
|
(def router
|
2018-09-07 21:16:59 +00:00
|
|
|
(pedestal/routing-interceptor
|
|
|
|
|
(http/router
|
|
|
|
|
[["/swagger.json"
|
|
|
|
|
{:get {:no-doc true
|
|
|
|
|
:swagger {:info {:title "my-api"
|
|
|
|
|
:description "with pedestal & reitit-http"}}
|
|
|
|
|
:handler (swagger/create-swagger-handler)}}]
|
|
|
|
|
|
2018-12-26 13:43:26 +00:00
|
|
|
["/interceptors"
|
|
|
|
|
{:swagger {:tags ["interceptors"]}
|
|
|
|
|
:interceptors [(interceptor 1)]}
|
|
|
|
|
|
|
|
|
|
["/number"
|
|
|
|
|
{:interceptors [(interceptor 10)]
|
|
|
|
|
:get {:interceptors [(interceptor 100)]
|
|
|
|
|
:handler (fn [req]
|
|
|
|
|
{:status 200
|
|
|
|
|
:body (select-keys req [:number])})}}]]
|
|
|
|
|
|
2018-09-07 21:16:59 +00:00
|
|
|
["/files"
|
|
|
|
|
{:swagger {:tags ["files"]}}
|
|
|
|
|
|
|
|
|
|
["/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"))})}}]]
|
|
|
|
|
|
|
|
|
|
["/math"
|
|
|
|
|
{:swagger {:tags ["math"]}}
|
|
|
|
|
|
|
|
|
|
["/plus"
|
|
|
|
|
{:get {:summary "plus with spec 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 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)}})}}]]]
|
|
|
|
|
|
2019-05-11 07:25:09 +00:00
|
|
|
{;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
|
|
|
|
|
;;:validate spec/validate ;; enable spec validation for route data
|
2019-05-11 11:25:25 +00:00
|
|
|
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
|
2019-05-11 07:25:09 +00:00
|
|
|
:exception pretty/exception
|
2019-05-11 11:25:25 +00:00
|
|
|
:data {:coercion reitit.coercion.spec/coercion
|
2018-09-07 21:16:59 +00:00
|
|
|
:muuntaja m/instance
|
2019-05-11 07:25:09 +00:00
|
|
|
:interceptors [;; swagger feature
|
|
|
|
|
swagger/swagger-feature
|
|
|
|
|
;; query-params & form-params
|
2018-09-07 21:16:59 +00:00
|
|
|
(parameters/parameters-interceptor)
|
|
|
|
|
;; content-negotiation
|
|
|
|
|
(muuntaja/format-negotiate-interceptor)
|
|
|
|
|
;; encoding response body
|
|
|
|
|
(muuntaja/format-response-interceptor)
|
2019-05-11 07:25:09 +00:00
|
|
|
;; exception handling
|
|
|
|
|
(exception/exception-interceptor)
|
2018-09-07 21:16:59 +00:00
|
|
|
;; 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)]}})
|
|
|
|
|
|
|
|
|
|
;; optional default ring handler (if no routes have matched)
|
|
|
|
|
(ring/routes
|
|
|
|
|
(swagger-ui/create-swagger-ui-handler
|
|
|
|
|
{:path "/"
|
2019-05-11 11:27:08 +00:00
|
|
|
:config {:validatorUrl nil
|
|
|
|
|
:operationsSorter "alpha"}})
|
2018-12-26 13:43:26 +00:00
|
|
|
(ring/create-resource-handler)
|
2018-09-07 21:16:59 +00:00
|
|
|
(ring/create-default-handler))))
|
|
|
|
|
|
|
|
|
|
(defn start []
|
2018-12-26 13:43:26 +00:00
|
|
|
(-> {:env :dev
|
|
|
|
|
::server/type :jetty
|
|
|
|
|
::server/port 3000
|
|
|
|
|
::server/join? false
|
|
|
|
|
;; no pedestal routes
|
|
|
|
|
::server/routes []
|
|
|
|
|
;; allow serving the swagger-ui styles & scripts from self
|
|
|
|
|
::server/secure-headers {:content-security-policy-settings
|
|
|
|
|
{:default-src "'self'"
|
|
|
|
|
:style-src "'self' 'unsafe-inline'"
|
|
|
|
|
:script-src "'self' 'unsafe-inline'"}}}
|
2019-05-23 15:53:27 +00:00
|
|
|
(server/default-interceptors)
|
2018-12-26 13:43:26 +00:00
|
|
|
;; use the reitit router
|
|
|
|
|
(pedestal/replace-last-interceptor router)
|
2019-05-23 15:53:27 +00:00
|
|
|
(server/dev-interceptors)
|
|
|
|
|
(server/create-server)
|
|
|
|
|
(server/start))
|
2018-09-07 21:16:59 +00:00
|
|
|
(println "server running in port 3000"))
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
|
(start))
|