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