From 5d91d44f6ab3789cab1b78b1e36f8ae4eea5ba08 Mon Sep 17 00:00:00 2001 From: Daniel Szmulewicz Date: Mon, 18 Sep 2023 12:33:05 +0300 Subject: [PATCH] Endpoint with middleware - Debug entry for handler listing all routes --- meyvn.edn | 2 +- src/system/components/endpoint.clj | 17 ++++++++++++----- src/system/components/handler.clj | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/meyvn.edn b/meyvn.edn index e4248b1..9f2f4ac 100644 --- a/meyvn.edn +++ b/meyvn.edn @@ -1,6 +1,6 @@ {:pom {:group-id "org.danielsz", :artifact-id "system", - :version "0.5.0", + :version "0.5.1-SNAPSHOT", :name "system"}, :build-properties {:project-build-sourceEncoding "UTF-8"}, :packaging {:uberjar {:graalvm {:enabled false, diff --git a/src/system/components/endpoint.clj b/src/system/components/endpoint.clj index 2997867..8f209fa 100644 --- a/src/system/components/endpoint.clj +++ b/src/system/components/endpoint.clj @@ -6,11 +6,18 @@ (defrecord Endpoint [routes middleware] component/Lifecycle (start [component] - (assoc component :routes (cond - (vector? routes) (if (not-empty middleware) - (ring/router routes {:reitit.middleware/transform #(into % middleware)}) - (ring/router routes)) - (and (ifn? routes) (satisfies? Router (routes component))) (routes component)))) + (let [transform (fn [routes] (mapv + (fn [entry] + (if (associative? entry) + (let [[k v] entry] + [k (assoc v :middleware middleware)]) + entry)) + routes))] + (assoc component :routes (cond + (vector? routes) (if (not-empty middleware) + (ring/router (transform routes)) + (ring/router routes)) + (and (ifn? routes) (satisfies? Router (routes component))) (routes component))))) (stop [component] (dissoc component :routes))) diff --git a/src/system/components/handler.clj b/src/system/components/handler.clj index aa9c8ff..dbf4951 100644 --- a/src/system/components/handler.clj +++ b/src/system/components/handler.clj @@ -20,9 +20,9 @@ (let [routes (map :routes (vals (endpoints component))) routers (apply merge-routers routes) handler (ring/ring-handler routers (default-handler component) options)] - (assoc component :handler handler))) + (assoc component :handler handler :debug (r/routes routers)))) (stop [component] - (dissoc component :handler))) + (dissoc component :handler :debug))) (defn new-handler [& {:keys [default-handler options]}]