Using liberator with Component from Stuart Sierra

98 views
Skip to first unread message

Rinat Abdullin

unread,
Feb 28, 2016, 5:13:09 AM2/28/16
to Liberator
Hi All,

I'm trying to use Liberator in the project which already uses Component from Stuart Sierra. It requires state to be local, while dependencies are passed as an argument.

What is the best way to create instances of Liberator api, while passing all dependencies down to handlers via arguments?

I apologise if my question is too naive or doesn't make sense. I just couldn't find the answer to it by going through Liberator docs and googling.


Best regards,
Rinat

Philipp Meier

unread,
Mar 2, 2016, 6:19:28 AM3/2/16
to Liberator


Am Sonntag, 28. Februar 2016 11:13:09 UTC+1 schrieb Rinat Abdullin:

I'm trying to use Liberator in the project which already uses Component from Stuart Sierra. It requires state to be local, while dependencies are passed as an argument.

What is the best way to create instances of Liberator api, while passing all dependencies down to handlers via arguments?

There are a couple of options and it depends on whether your web server, say jetty, is a component itself.

In general there are three options:

* assoc dependencies to the request. This can be done in some middleware. The resource created with liberator can pull the values from the request where necessary
* use parametrized resources. In this case the dependencies need to be passed to the resource function to create the handler. This typically has impact on the way that you defined the routing
* wrap every liberator resource in a component and have component pass in the dependencies. You will need another component that depends on all the resources and does the routing. As an alternative, that component could iterator over the system and collect all the liberator resources. This is what https://modularity.org/ suggests
* use dynamic binding or global vars to hold values for the resources. This typically neither scales nor compose and is not recommended by me.

All approaches have different tradeoffs regarding modularity and testing. You will need to find your preferred poison here. I'm pretty sure that there are more possibilities.

I apologise if my question is too naive or doesn't make sense. I just couldn't find the answer to it by going through Liberator docs and googling.

The question is actually a very good one. 

Rinat Abdullin

unread,
Mar 4, 2016, 9:30:52 AM3/4/16
to clojure-...@googlegroups.com
Philipp,

thank you very much for this detailed explanation. Plugging in through the middleware was the answer to get started for me.

Best regards,
Rinat

Rinat Abdullin | Writer at Abdullin.com

--
You received this message because you are subscribed to a topic in the Google Groups "Liberator" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-liberator/nNSdGfEHsMc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-libera...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages