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

87 lines
3.4 KiB
Clojure
Raw Normal View History

2018-04-19 19:58:59 +00:00
(ns example.server
(:require [reitit.ring :as ring]
[reitit.swagger :as swagger]
2018-05-14 05:21:47 +00:00
[reitit.swagger-ui :as swagger-ui]
2018-07-22 13:27:20 +00:00
[reitit.ring.coercion :as coercion]
[reitit.coercion.spec]
2018-07-21 13:32:43 +00:00
[reitit.ring.middleware.muuntaja :as muuntaja]
2018-07-22 13:27:20 +00:00
[reitit.ring.middleware.exception :as exception]
2018-07-30 08:10:46 +00:00
[reitit.ring.middleware.multipart :as multipart]
2018-07-22 13:27:20 +00:00
[ring.middleware.params :as params]
2018-04-19 19:58:59 +00:00
[ring.adapter.jetty :as jetty]
2018-07-30 08:10:46 +00:00
[muuntaja.core :as m]
[clojure.java.io :as io]))
2018-04-19 19:58:59 +00:00
(def app
(ring/ring-handler
(ring/router
2018-07-22 13:27:20 +00:00
[["/swagger.json"
2018-05-07 06:03:40 +00:00
{:get {:no-doc true
:swagger {:info {:title "my-api"}}
:handler (swagger/create-swagger-handler)}}]
2018-04-19 19:58:59 +00:00
2018-07-30 08:10:46 +00:00
["/files"
{:swagger {:tags ["files"]}}
["/upload"
{:post {:summary "upload a file"
:parameters {:multipart {:file multipart/temp-file-part}}
:responses {200 {:body {:file multipart/temp-file-part}}}
:handler (fn [{{{:keys [file]} :multipart} :parameters}]
{:status 200
:body {:file 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-07-22 13:27:20 +00:00
["/math"
{:swagger {:tags ["math"]}}
2018-05-07 05:59:21 +00:00
2018-05-07 06:03:40 +00:00
["/plus"
{:get {:summary "plus with spec query parameters"
2018-05-07 06:03:40 +00:00
: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)}})}}]]]
2018-04-19 19:58:59 +00:00
2018-07-22 13:27:20 +00:00
{:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:middleware [;; query-params & form-params
params/wrap-params
;; 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
2018-07-30 08:10:46 +00:00
coercion/coerce-request-middleware
;; multipart
multipart/multipart-middleware]}})
2018-04-29 12:18:30 +00:00
(ring/routes
2018-07-22 13:27:20 +00:00
(swagger-ui/create-swagger-ui-handler {:path "/"})
2018-04-29 12:18:30 +00:00
(ring/create-default-handler))))
2018-04-19 19:58:59 +00:00
(defn start []
(jetty/run-jetty #'app {:port 3000, :join? false})
(println "server running in port 3000"))
(comment
(start))