Fork me on GitHub
#pedestal
<
2023-04-12
>
rossputin19:04:42

(re-posting from the clojure channel) Hi all - I’m hoping someone can point me to an example of passing state into pedestal so it will be available in all functions associated with routes - tutorials or docs etc - thanks!

rossputin19:04:07

For context - I’m trying to avoid having an atom and using that sprinkled around the code

Mark Wardle19:04:13

You can create an interceptor which could be used by one or more, or all of your routes that injects whatever you want into the context or the request, and so make it available to all subsequent interceptors. e.g. see http://pedestal.io/reference/interceptors

Mark Wardle19:04:43

There they have an example of attaching a database. Generally, I think most people use some form of component framework such as integrant, or component, or mount, so that you can pass in your dependencies (E.g. a database conn pool) to your server on startup, and then build your service to include an interceptor than pushes the conn into the context/request. That's what I do mostly. But here is an example from one of my projects, which takes a dependency (for this it is 'svc') and ensures it will be available by building an interceptor using it.

rossputin19:04:52

Thanks very much for the help - off to read up!

Mark Wardle20:04:09

No problem. The trick is to realise that pedestal takes functions and maps and turns them into interceptors for you, but if you are building one yourself and adding it into your interceptor chain then you need to manually make an interceptor using ‘interceptor’.

👍 2
Mark Wardle06:04:43

I just looked through my code example and noticed some of the interceptors can sometimes return nil - that’ll result in a server error when in fact it should be a 404. They should always return something.

rossputin06:04:22

thanks for the follow, definitely going to save me time