pedestal

sirwobin 2025-05-28T10:00:19.244469Z

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.

sirwobin 2025-05-28T10:04:17.301859Z

It would be nice to be able to add something to the interceptor context bindings, for example.

simongray 2025-05-28T10:20:26.303619Z

Add an interceptor somewhere in the chain that assocs it into the context?

sirwobin 2025-05-28T10:21:27.178359Z

And how should the interceptor reference stuff in the component system?

simongray 2025-05-28T10:22:49.371679Z

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.

simongray 2025-05-28T10:23:22.391999Z

All interceptors that access the database should always look for the :db key in the ctx.

sirwobin 2025-05-28T10:25:12.550329Z

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.

sirwobin 2025-05-28T10:25:22.906279Z

Still, this works! Thank you Simon.

simongray 2025-05-28T10:26:59.163739Z

(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 🀷

sirwobin 2025-05-28T10:28:40.072949Z

That's what I'd prefer to do. How can an interceptor reference the service map or any service configuration?

simongray 2025-05-28T10:29:39.740559Z

Same deal. Put it somewhere in an atom and make an interceptor that assocs it.

πŸ‘ 1
sirwobin 2025-05-28T11:29:10.706199Z

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.

simongray 2025-05-28T11:36:14.533499Z

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

sirwobin 2025-05-28T11:36:59.376419Z

Perfect. Thanks again! πŸ™‚

simongray 2025-05-28T11:37:25.174799Z

But the better solution is probably to explicitly define your own vector of default interceptors.

simongray 2025-05-28T11:37:33.649319Z

and assoc that

sirwobin 2025-05-28T11:37:44.452329Z

This feels more like the dependency injection that is the spirit of Component.

hlship 2025-05-28T14:10:58.504009Z

The answer for this is much improved in 0.8. Even in 0.7, you can provide an initial context that includes injected components.

sirwobin 2025-05-28T14:12:48.425879Z

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?

hlship 2025-05-28T16:09:07.531489Z

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.

hlship 2025-05-28T16:22:06.499479Z

But what's coming in 0.8 is much better: https://pedestal.io/pedestal/0.8/guides/pedestal-component-2.html

sirwobin 2025-05-30T11:36:10.421409Z

That looks very nice indeed! πŸ™‚ Looking forward to 0.8