Skip to content

Commit

Permalink
feat(module): use module.registry for passing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Dec 24, 2021
1 parent 87afe9f commit 7c74c8b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]])
32 changes: 23 additions & 9 deletions src/duct/module/reitit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}))))
4 changes: 2 additions & 2 deletions test/duct/module/reitit_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 []}}})

Expand Down

0 comments on commit 7c74c8b

Please sign in to comment.