# Providers and prepare vs boot

**URL:** <https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113>\
**Category:** questions\
**Created:** [February 3, 2025, 5:13pm UTC](https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113 "2025-02-03T17:13:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![svoop](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/svoop/32/443_2.png) [@svoop](https://discourse.hanakai.org/u/svoop)\
**Post date:** [February 3, 2025, 5:13pm UTC](https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113/1 "2025-02-03T17:13:19Z")

</div>

I have a little prepare/boot misunderstanding it seems:

Unlike the default `.env` approach, I’m loading access tokens using my dry-credentials gem as a provider:

```ruby
# config/providers/credentials.rb

Hanami.app.register_provider :credentials do
  prepare do
    require "dry-credentials"

    Dry::Credentials::Extension.new.then do |credentials|
      # gem config stuff

      register "credentials", credentials
    end
  end
end

```

The Redis URL is such a token and therefore, I can’t setup the Rack session `:redis` backend in `config/app.rb` but have to do it at a later point in another provider which makes sure, the credentials provider is prepped:

```ruby
# config/providers/session.rb

Hanami.app.register_provider :session do
  prepare do
    target.prepare(:credentials)

    target.config.actions.sessions = :redis, {
      redis_server: "#{target['credentials'].current_cache_url}/#{target.settings.cache_namespace}:session",
      expire_after: target.settings.session_timeout
    }
  end
end

```

This works when serving the site.

But the tests fail since the `:session` provider prepare block is not called when `spec_helper.rb` hits `require hanami/prepare`. (This require does not run all provider prepare blocks as one might think.)

Easy fix: Do a `require hanami/boot` instead at the cost of booting the entire app on every (even single) test run.

Do you see a better way to tackle this?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [February 3, 2025, 10:47pm UTC](https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113/2 "2025-02-03T22:47:46Z")

</div>

For speed reasons, I think I would prefer to keep provider loading lazy. This came up earlier, and [here’s my solution](https://discourse.hanakai.org/t/loading-providers-for-specs/1060/3)

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/alassek/32/532_2.png) [@alassek](https://discourse.hanakai.org/u/alassek)\
**Post date:** [February 3, 2025, 10:56pm UTC](https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113/3 "2025-02-03T22:56:34Z")

</div>

Not directly germane to your question, but my mental model for providers is that `prepare` is for dependencies, and `start` is for doing the thing. So I would write yours like this:

```ruby
# config/providers/credentials.rb

Hanami.app.register_provider :credentials do
  prepare do
    require "dry-credentials"
  end

  start do
    Dry::Credentials::Extension.new.then do |credentials|
      # gem config stuff

      register "credentials", credentials
    end
  end
end

```

```ruby
# config/providers/session.rb

Hanami.app.register_provider :session do
  prepare do
    target.start(:credentials)
  end

  start do
    target.config.actions.sessions = :redis, {
      redis_server: "#{target['credentials'].current_cache_url}/#{target.settings.cache_namespace}:session",
      expire_after: target.settings.session_timeout
    }
  end
end

```

---

<div class="post-metadata">

**Author:** ![svoop](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/svoop/32/443_2.png) [@svoop](https://discourse.hanakai.org/u/svoop)\
**Post date:** [February 4, 2025, 11:21am UTC](https://discourse.hanakai.org/t/providers-and-prepare-vs-boot/1113/4 "2025-02-04T11:21:59Z")

</div>

Funny enough, that’s how my providers looked like before intermingling the app prepare and the provider prepares. 😀
