From 7c74c8be0e6ef9e2a59e08e03444cc67298a8bad Mon Sep 17 00:00:00 2001 From: tami5 Date: Fri, 24 Dec 2021 22:21:44 +0300 Subject: [PATCH] feat(module): use module.registry for passing functions --- README.md | 4 ++-- project.clj | 5 +++-- src/duct/module/reitit.clj | 32 +++++++++++++++++++++++--------- test/duct/module/reitit_test.clj | 4 ++-- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 059959e..7cf6dbe 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ Full configuration demo: :methods [:get :post :delete :options]} :duct.module.reitit/opts - {:coercion :data-spec ; default nil + {:coercion data-spec ; default nil :environment {:db #ig/ref :foo/database} ; default nil - :middlewares []}} } + :middlewares []}}} ; default empty ``` ### Keys diff --git a/project.clj b/project.clj index cac8528..a0e6aff 100644 --- a/project.clj +++ b/project.clj @@ -7,11 +7,12 @@ [ring-logger "1.0.1"] [metosin/reitit "0.5.15"] [duct/core "0.7.0"] + [duct/logger.timbre "0.5.0"] [integrant "0.7.0"] [medley "1.0.0"]] - :plugins [[cider/cider-nrepl "0.27.3"]] :profiles {:repl {:source-paths ["dev/src"] :resource-paths ["dev/resources"] :dependencies [[clj-http/clj-http "3.12.3"] - [ring/ring-mock "0.4.0"]]}}) + [ring/ring-mock "0.4.0"]]}} + :plugins [[cider/cider-nrepl "0.27.3"]]) diff --git a/src/duct/module/reitit.clj b/src/duct/module/reitit.clj index ae26505..b966825 100644 --- a/src/duct/module/reitit.clj +++ b/src/duct/module/reitit.clj @@ -68,16 +68,30 @@ (defn- merge-with-defaults [config] (merge default-config config)) -(defn- to-map [registry] - (-> #(assoc %1 (first %3) (second %3)) - (reduce-kv {} registry))) +(defn- registry->duct-config [registry] + (reduce-kv + (fn [m _ v] + (assoc m (first v) (second v))) + {} + registry)) + +(defn- registry->duct-registry [registry] + (reduce-kv + (fn [m k v] + (assoc m k (ig/ref (first v)))) + {} + registry)) (defmethod init-key :duct.module/reitit [_ {:keys [routes registry]}] - (fn [{:duct.module.reitit/keys [cors opts] :as config}] + (fn [config] (let [config (merge-with-defaults config) registry (resolve-registry registry config) - extra (to-map registry) - router {:routes routes :registry registry :opts opts} - update (-> extra - (assoc :duct.router/reitit router))] - (duct/merge-configs config update)))) + extra (registry->duct-config registry) + merge #(duct/merge-configs config (merge extra %)) + router {:routes routes + :cors (ig/ref ::cors) + :registry (ig/ref ::registry) + :opts (ig/ref ::opts)}] + (merge + {::registry (registry->duct-registry registry) + :duct.router/reitit router})))) diff --git a/test/duct/module/reitit_test.clj b/test/duct/module/reitit_test.clj index 9d38229..684e46d 100644 --- a/test/duct/module/reitit_test.clj +++ b/test/duct/module/reitit_test.clj @@ -32,8 +32,8 @@ {:origin [#".*"] :methods [:get :post :delete :options]} - :duct.module.reitit/options - {:coercion :data-spec ; default nil + :duct.module.reitit/opts + {:coercion data-spec ; default nil :environment {:db (ig/ref :foo/database)} ; default nil :middlewares []}}})