return json

This commit is contained in:
Tommi Reiman 2018-09-03 18:52:57 +03:00
parent 13ceab524d
commit 048f7e48b6
2 changed files with 11 additions and 7 deletions

View file

@ -3,18 +3,19 @@
[reitit.ring :as ring] [reitit.ring :as ring]
[reitit.interceptor.sieppari] [reitit.interceptor.sieppari]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[muuntaja.interceptor]
[clojure.core.async :as a] [clojure.core.async :as a]
[manifold.deferred :as d] [manifold.deferred :as d]
[promesa.core :as p])) [promesa.core :as p]))
(defn interceptor [f x] (defn interceptor [f x]
{:enter (fn [ctx] (f (update-in ctx [:request :via] (fnil conj []) x))) {:enter (fn [ctx] (f (update-in ctx [:request :via] (fnil conj []) {:enter x})))
:leave (fn [ctx] (f (update-in ctx [:response :body] str "\n<- " x)))}) :leave (fn [ctx] (f (update-in ctx [:response :body] conj {:leave x})))})
(defn handler [f] (defn handler [f]
(fn [{:keys [via]}] (fn [{:keys [via]}]
(f {:status 200, (f {:status 200,
:body (str (apply str (map #(str "-> " % "\n") via)) " hello!")}))) :body (conj via :handler)})))
(def <sync> identity) (def <sync> identity)
(def <future> #(future %)) (def <future> #(future %))
@ -54,7 +55,8 @@
:handler (handler <promesa>)}}]]) :handler (handler <promesa>)}}]])
(ring/create-default-handler) (ring/create-default-handler)
{:executor reitit.interceptor.sieppari/executor})) {:executor reitit.interceptor.sieppari/executor
:interceptors [(muuntaja.interceptor/format-interceptor)]}))
(defn start [] (defn start []
(jetty/run-jetty #'app {:port 3000, :join? false, :async? true}) (jetty/run-jetty #'app {:port 3000, :join? false, :async? true})

View file

@ -26,10 +26,11 @@
["/upload" ["/upload"
{:post {:summary "upload a file" {:post {:summary "upload a file"
:parameters {:multipart {:file multipart/temp-file-part}} :parameters {:multipart {:file multipart/temp-file-part}}
:responses {200 {:body {:file multipart/temp-file-part}}} :responses {200 {:body {:name string?, :size int?}}}
:handler (fn [{{{:keys [file]} :multipart} :parameters}] :handler (fn [{{{:keys [file]} :multipart} :parameters}]
{:status 200 {:status 200
:body {:file file}})}}] :body {:name (:filename file)
:size (:size file)}})}}]
["/download" ["/download"
{:get {:summary "downloads a file" {:get {:summary "downloads a file"
@ -37,7 +38,8 @@
:handler (fn [_] :handler (fn [_]
{:status 200 {:status 200
:headers {"Content-Type" "image/png"} :headers {"Content-Type" "image/png"}
:body (io/input-stream (io/resource "reitit.png"))})}}]] :body (io/input-stream
(io/resource "reitit.png"))})}}]]
["/math" ["/math"
{:swagger {:tags ["math"]}} {:swagger {:tags ["math"]}}