Ring uses Middleware Endpoint & polish

This commit is contained in:
Tommi Reiman 2017-09-08 08:29:08 +03:00
parent ac8a7b5cd2
commit 3bc42a8de8

View file

@ -6,7 +6,6 @@
(def http-methods #{:get :head :patch :delete :options :post :put}) (def http-methods #{:get :head :patch :delete :options :post :put})
(defrecord Methods [get head post put delete trace options connect patch any]) (defrecord Methods [get head post put delete trace options connect patch any])
(defrecord Endpoint [meta handler])
(defn- group-keys [meta] (defn- group-keys [meta]
(reduce-kv (reduce-kv
@ -56,30 +55,22 @@
(update acc method expand opts) (update acc method expand opts)
acc)) meta http-methods)]) acc)) meta http-methods)])
(defn compile-handler [[path meta] opts] (defn compile-result [[path meta] opts]
(let [[top childs] (group-keys meta)] (let [[top childs] (group-keys meta)]
(if-not (seq childs) (if-not (seq childs)
(map->Methods (let [middleware (middleware/compile-result [path top] opts)]
{:any (map->Endpoint (map->Methods {:any (middleware/compile-result [path top] opts)}))
{:handler (middleware/compile-handler [path top] opts) (let [any-handler (if (:handler top) (middleware/compile-result [path meta] opts))]
:meta top})})
(let [any-handler (if (:handler top) (middleware/compile-handler [path meta] opts))]
(reduce-kv (reduce-kv
(fn [acc method meta] (fn [acc method meta]
(let [meta (meta-merge top meta) (let [meta (meta-merge top meta)]
handler (middleware/compile-handler [path meta] opts method)] (assoc acc method (middleware/compile-result [path meta] opts method))))
(assoc acc method (map->Endpoint (map->Methods {:any any-handler})
{:handler handler
:meta meta}))))
(map->Methods
{:any (map->Endpoint
{:handler (if (:handler top) (middleware/compile-handler [path meta] opts))
:meta top})})
childs))))) childs)))))
(defn router (defn router
([data] ([data]
(router data nil)) (router data nil))
([data opts] ([data opts]
(let [opts (meta-merge {:coerce coerce-handler, :compile compile-handler} opts)] (let [opts (meta-merge {:coerce coerce-handler, :compile compile-result} opts)]
(reitit/router data opts)))) (reitit/router data opts))))