update example

This commit is contained in:
Tommi Reiman 2018-09-03 19:31:42 +03:00
parent dda76fc7f1
commit 292d3fd76d

View file

@ -6,43 +6,42 @@
[reitit.ring :as ring])) [reitit.ring :as ring]))
(defn interceptor [x] (defn interceptor [x]
{:enter (fn [ctx] (println ">>" x) ctx) {:enter (fn [ctx] (update-in ctx [:request :via] (fnil conj []) {:enter x}))
:leave (fn [ctx] (println "<<" x) ctx)}) :leave (fn [ctx] (update-in ctx [:response :body] conj {:leave x}))})
(defn handler [_] (defn handler [{:keys [via]}]
(println "handler")
{:status 200, {:status 200,
:body "pong"}) :body (conj via :handler)})
(def async-handler (def async-handler
{:enter (fn [{:keys [request] :as ctx}] {:enter (fn [{:keys [request] :as ctx}]
(a/go (a/go (assoc ctx :response (handler request))))})
(assoc ctx :response (handler request))))})
(def routing-interceptor (def routing-interceptor
(pedestal/routing-interceptor (pedestal/routing-interceptor
(http/router (http/router
["/api" ["/api"
{:interceptors [[interceptor :api] {:interceptors [(interceptor :api)
[interceptor :ipa]]} (interceptor :ipa)]}
["/sync" ["/sync"
{:interceptors [[interceptor :sync]] {:interceptors [(interceptor :sync)]
:get {:interceptors [[interceptor :get]] :get {:interceptors [(interceptor :get)]
:handler handler}}] :handler handler}}]
["/async" ["/async"
{:interceptors [[interceptor :async]] {:interceptors [(interceptor :async)]
:get {:interceptors [[interceptor :get] async-handler]}}]] :get {:interceptors [(interceptor :get) async-handler]}}]]
;; optional interceptors for all matched routes ;; optional interceptors for all matched routes
{:data {:interceptors [[interceptor :router]]}}) {:data {:interceptors [(interceptor :router)]}})
;; optional default ring handler (if no routes have matched) ;; optional default ring handler (if no routes have matched)
(ring/create-default-handler) (ring/create-default-handler)
;; optional top-level routes for both routes & default route ;; optional top-level routes for both routes & default route
{:interceptors [[interceptor :top]]})) {:interceptors [(muuntaja.interceptor/format-interceptor)
(interceptor :top)]}))
(defonce server (atom nil)) (defonce server (atom nil))