How should I inject information about a component into an interceptor? I'm using Stuart Sierra components and don't see an obvious way to connect them.
It would be nice to be able to add something to the interceptor context bindings, for example.
Add an interceptor somewhere in the chain that assocs it into the context?
And how should the interceptor reference stuff in the component system?
Reference how? As an example, I do something like this to access the db:
(def with-db
(interceptor
{:name ::with-db
:enter (fn [ctx]
(assoc ctx :db (d/db @db/conn)))}))
So you can either add this to each relevant interceptor chain or to the default interceptors.All interceptors that access the database should always look for the :db key in the ctx.
I see, you're relying on an atom in the db namespace, which provides the point of connection. I was rather hoping for a mechanism in the pedestal service map which might allow me to inject it into the context.
Still, this works! Thank you Simon.
(not an atom AFAIK, itβs a Datalevin database connection, but it is being dereffed) You could always add it to the service map if you want and create an interceptor that fetches it from there π€·
That's what I'd prefer to do. How can an interceptor reference the service map or any service configuration?
Same deal. Put it somewhere in an atom and make an interceptor that assocs it.
Hmm, looks like I should be able to modify the default interceptors to add what I need https://pedestal.io/pedestal/0.7/reference/default-interceptors.html. Here one has a reference to the service map and other component level setup, so that other components and their setup can be injected at service creating time.
Yes. I do it like this if you want an example: https://github.com/bedebok/prayer-app/blob/master/src/dk/cst/prayer/web/backend.clj#L66-L69
Perfect. Thanks again! π
But the better solution is probably to explicitly define your own vector of default interceptors.
and assoc that
This feels more like the dependency injection that is the spirit of Component.
The answer for this is much improved in 0.8. Even in 0.7, you can provide an initial context that includes injected components.
That's great to hear, thank you! I have it working nicely in 0.7 using an interceptor as described in that doc. Did I miss another way to accomplish this in 0.7?
Well, if you have a component to start the Pedestal service, that component can have dependencies, and it can provide an initial context that contains those dependencies.
But what's coming in 0.8 is much better: https://pedestal.io/pedestal/0.8/guides/pedestal-component-2.html
That looks very nice indeed! π Looking forward to 0.8