mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 08:51:12 +00:00
update example
This commit is contained in:
parent
dda76fc7f1
commit
292d3fd76d
1 changed files with 14 additions and 15 deletions
|
|
@ -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))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue