My recommendation is to use a closure. So I'd write your example as:
(defn username-endpoint [{:keys [db]}]
(routes
(GET "/:username" [username]
(let [user (users/get-user db username)]
(str "<h1>Hello " (:name user) "</h1>")))))
So you pass your configuration map into the endpoint function, which
returns a handler.
You can then wrap this in a component:
(defrecord EndpointComponent [build-routes]
component/Lifecycle
(start [component]
(if (:routes component)
component
(assoc component :routes (build-routes component))))
(stop [component]
(dissoc component :routes)))
Incidentally, the above code is taken directly from Duct
<https://github.com/weavejester/duct>, a template and small supporting
library I've written for building component-based web apps.
I've also written a blog article
<https://www.booleanknot.com/blog/2015/05/22/structuring-clojure-web-apps.html>
around
general best practice for this type of style.
- James
On 8 June 2015 at 22:51, Dru Sellers <[email protected]> wrote:
> So, I guess I am a bit lost, how does someone actually use component? I
> have an application all set up with it and it seems to be working as I
> would expect but Stuart seems to be steering me in a different direction.
>
> https://github.com/stuartsierra/component/pull/35
>
> https://github.com/stuartsierra/component/issues/34
>
> So I'll try and paint a full picture.
> https://gist.github.com/drusellers/8109dce4b9fb19c14ebb
>
> I know compojure and component / reloaded may not play well, but I'm
> trying to figure out how to best use the system var. Am I close, I'd love
> to give back a decent PR to the README.md of the component repo to help
> others as they come along.
>
> -d
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.