Skip to content

Commit

Permalink
Endpoint with middleware
Browse files Browse the repository at this point in the history
- Debug entry for handler listing all routes
  • Loading branch information
danielsz committed Sep 18, 2023
1 parent e61487e commit 5d91d44
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion meyvn.edn
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
17 changes: 12 additions & 5 deletions src/system/components/endpoint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down
4 changes: 2 additions & 2 deletions src/system/components/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]}]
Expand Down

0 comments on commit 5d91d44

Please sign in to comment.