mirror of
https://github.com/metosin/reitit.git
synced 2025-12-17 00:11:11 +00:00
pedestal+reitit-http+swagger
This commit is contained in:
parent
d3baab9476
commit
9260f3b3fd
6 changed files with 191 additions and 0 deletions
11
examples/pedestal-swagger/.gitignore
vendored
Normal file
11
examples/pedestal-swagger/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
/target
|
||||||
|
/classes
|
||||||
|
/checkouts
|
||||||
|
pom.xml
|
||||||
|
pom.xml.asc
|
||||||
|
*.jar
|
||||||
|
*.class
|
||||||
|
/.lein-*
|
||||||
|
/.nrepl-port
|
||||||
|
.hgignore
|
||||||
|
.hg/
|
||||||
23
examples/pedestal-swagger/README.md
Normal file
23
examples/pedestal-swagger/README.md
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
# Pedestal with reitit-http & Swagger example
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```clj
|
||||||
|
> lein repl
|
||||||
|
(start)
|
||||||
|
```
|
||||||
|
|
||||||
|
To test the endpoints using [httpie](https://httpie.org/):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
http GET :3000/math/plus x==1 y==20
|
||||||
|
http POST :3000/math/spec/plus x:=1 y:=20
|
||||||
|
|
||||||
|
http GET :3000/swagger.json
|
||||||
|
```
|
||||||
|
|
||||||
|
<img src="https://raw.githubusercontent.com/metosin/reitit/master/examples/pedestal-swagger/swagger.png" />
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Copyright © 2018 Metosin Oy
|
||||||
7
examples/pedestal-swagger/project.clj
Normal file
7
examples/pedestal-swagger/project.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
(defproject ring-example "0.1.0-SNAPSHOT"
|
||||||
|
:description "Reitit-http with pedestal"
|
||||||
|
:dependencies [[org.clojure/clojure "1.9.0"]
|
||||||
|
[io.pedestal/pedestal.service "0.5.4"]
|
||||||
|
[io.pedestal/pedestal.jetty "0.5.4"]
|
||||||
|
[metosin/reitit "0.2.1"]]
|
||||||
|
:repl-options {:init-ns example.server})
|
||||||
BIN
examples/pedestal-swagger/resources/reitit.png
Normal file
BIN
examples/pedestal-swagger/resources/reitit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 494 KiB |
112
examples/pedestal-swagger/src/example/server.clj
Normal file
112
examples/pedestal-swagger/src/example/server.clj
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
(ns example.server
|
||||||
|
(:require [io.pedestal.http]
|
||||||
|
[reitit.interceptor.pedestal :as pedestal]
|
||||||
|
[reitit.ring :as ring]
|
||||||
|
[reitit.http :as http]
|
||||||
|
[reitit.swagger :as swagger]
|
||||||
|
[reitit.swagger-ui :as swagger-ui]
|
||||||
|
[reitit.http.coercion :as coercion]
|
||||||
|
[reitit.coercion.spec :as spec-coercion]
|
||||||
|
[reitit.http.interceptors.parameters :as parameters]
|
||||||
|
[reitit.http.interceptors.muuntaja :as muuntaja]
|
||||||
|
[reitit.http.interceptors.exception :as exception]
|
||||||
|
[reitit.http.interceptors.multipart :as multipart]
|
||||||
|
[muuntaja.core :as m]
|
||||||
|
[clojure.java.io :as io]))
|
||||||
|
|
||||||
|
(def routing-interceptor
|
||||||
|
(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)}}]
|
||||||
|
|
||||||
|
["/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)}})}}]]]
|
||||||
|
|
||||||
|
{:data {:coercion spec-coercion/coercion
|
||||||
|
:muuntaja m/instance
|
||||||
|
:interceptors [;; query-params & form-params
|
||||||
|
(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)]}})
|
||||||
|
|
||||||
|
;; optional default ring handler (if no routes have matched)
|
||||||
|
(ring/routes
|
||||||
|
(swagger-ui/create-swagger-ui-handler
|
||||||
|
{:path "/"
|
||||||
|
:config {:validatorUrl nil}})
|
||||||
|
(ring/create-default-handler))))
|
||||||
|
|
||||||
|
(defonce server (atom nil))
|
||||||
|
|
||||||
|
(defn start []
|
||||||
|
(when @server
|
||||||
|
(io.pedestal.http/stop @server)
|
||||||
|
(println "server stopped"))
|
||||||
|
(-> {:env :prod
|
||||||
|
:io.pedestal.http/routes []
|
||||||
|
:io.pedestal.http/resource-path "/public"
|
||||||
|
:io.pedestal.http/type :jetty
|
||||||
|
:io.pedestal.http/port 3000}
|
||||||
|
(merge {:env :dev
|
||||||
|
:io.pedestal.http/join? false
|
||||||
|
:io.pedestal.http/allowed-origins {:creds true :allowed-origins (constantly true)}})
|
||||||
|
(pedestal/default-interceptors routing-interceptor)
|
||||||
|
io.pedestal.http/dev-interceptors
|
||||||
|
io.pedestal.http/create-server
|
||||||
|
io.pedestal.http/start
|
||||||
|
(->> (reset! server)))
|
||||||
|
(println "server running in port 3000"))
|
||||||
|
|
||||||
|
(comment
|
||||||
|
(start))
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
(ns reitit.interceptor.pedestal
|
||||||
|
(:require [io.pedestal.interceptor.chain :as chain]
|
||||||
|
[io.pedestal.interceptor :as interceptor]
|
||||||
|
[io.pedestal.http :as http]
|
||||||
|
[reitit.interceptor]
|
||||||
|
[reitit.http])
|
||||||
|
(:import (reitit.interceptor Executor)))
|
||||||
|
|
||||||
|
(def pedestal-executor
|
||||||
|
(reify
|
||||||
|
Executor
|
||||||
|
(queue [_ interceptors]
|
||||||
|
(->> interceptors
|
||||||
|
(map (fn [{:keys [::interceptor/handler] :as interceptor}]
|
||||||
|
(or handler interceptor)))
|
||||||
|
(map interceptor/interceptor)))
|
||||||
|
(enqueue [_ context interceptors]
|
||||||
|
(chain/enqueue context interceptors))))
|
||||||
|
|
||||||
|
(defn routing-interceptor
|
||||||
|
([router]
|
||||||
|
(routing-interceptor router nil))
|
||||||
|
([router default-handler]
|
||||||
|
(routing-interceptor router default-handler nil))
|
||||||
|
([router default-handler {:keys [interceptors]}]
|
||||||
|
(interceptor/interceptor
|
||||||
|
(reitit.http/routing-interceptor
|
||||||
|
router
|
||||||
|
default-handler
|
||||||
|
{:executor pedestal-executor
|
||||||
|
:interceptors interceptors}))))
|
||||||
|
|
||||||
|
(defn default-interceptors [spec router]
|
||||||
|
(-> spec
|
||||||
|
(assoc ::http/routes [])
|
||||||
|
(http/default-interceptors)
|
||||||
|
(update ::http/interceptors (comp vec butlast))
|
||||||
|
(update ::http/interceptors conj router)))
|
||||||
Loading…
Reference in a new issue