Skip to content

feat!: Switch to Axum's FromRef for custom state - #250

Merged
spencewenski merged 1 commit into
mainfrom
gh-249-custom-state-from-ref
Jun 30, 2024
Merged

feat!: Switch to Axum's FromRef for custom state#250
spencewenski merged 1 commit into
mainfrom
gh-249-custom-state-from-ref

Conversation

@spencewenski

@spencewenski spencewenski commented Jun 29, 2024

Copy link
Copy Markdown
Member

Our previous approach to allowing consumers to provide custom
state/context was to embed it as a field in Roadster's AppContext.
This is not how Axum recommends libraries support custom state, which
makes it difficult (impossible?) to integrate Roadster with other
Axum libraries.

The recommended approach is to keep the state generic, and add a type
constraint to require Roadster's state to be able to be retrieved from
the custom state using the
FromRef
trait. See the following for more details: https://docs.rs/axum/latest/axum/extract/struct.State.html#for-library-authors

One example of an integration that was blocked by our non-recommended
approach: Leptos requires the app's state provide Leptos's state using
FromRef. In order to support Leptos with our previous approach, we
would have needed to add the Leptos state directly to Roadster's
AppContext. This adds a dependency on Roadster for consumers who may
want to use Leptos, and also adds a direct dependency between Roadster
and Leptos (though, it would be optional behind a feature flag). This
maybe could work in the short term, but this approach doesn't scale to
other libraries -- Roadster would need to follow a similar approach for
all other libraries consumers may want to use. Instead, it's better to
simply follow Axum's recommendation to provide maximum flexibility to
consumers.

@spencewenski
spencewenski force-pushed the gh-249-custom-state-from-ref branch from 3c29326 to 4529d65 Compare June 29, 2024 09:36
@spencewenski spencewenski changed the title Gh 249 custom state from ref feat: Use Axum's FromRef to allow custom state Jun 29, 2024
@codecov

codecov Bot commented Jun 29, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 47.43326% with 256 lines in your changes missing coverage. Please review.

Project coverage is 43.41%. Comparing base (04632d8) to head (5513855).

Files Coverage Δ
src/config/health_check/mod.rs 100.00% <100.00%> (ø)
src/config/service/http/default_routes.rs 100.00% <100.00%> (ø)
src/config/service/http/initializer.rs 100.00% <100.00%> (ø)
src/config/service/http/middleware.rs 100.00% <100.00%> (ø)
src/config/service/mod.rs 100.00% <100.00%> (ø)
src/health_check/default.rs 100.00% <100.00%> (ø)
src/health_check/mod.rs 0.00% <ø> (ø)
src/service/http/initializer/default.rs 100.00% <100.00%> (ø)
src/service/http/middleware/default.rs 100.00% <100.00%> (ø)
src/service/http/middleware/mod.rs 100.00% <ø> (ø)
... and 37 more

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 04632d8...5513855. Read the comment docs.

@spencewenski
spencewenski force-pushed the gh-249-custom-state-from-ref branch 2 times, most recently from ce150d5 to 46abba9 Compare June 29, 2024 09:48
@spencewenski spencewenski changed the title feat: Use Axum's FromRef to allow custom state feat!: Use Axum's FromRef to allow custom state Jun 29, 2024
@spencewenski
spencewenski force-pushed the gh-249-custom-state-from-ref branch 2 times, most recently from 97ffd4d to 1dc5fbc Compare June 29, 2024 10:29
@spencewenski spencewenski changed the title feat!: Use Axum's FromRef to allow custom state feat!: Use Axum's FromRef to allow custom state Jun 29, 2024
@spencewenski
spencewenski force-pushed the gh-249-custom-state-from-ref branch 2 times, most recently from e5b0714 to 7b5d176 Compare June 29, 2024 18:51
Our previous approach to allowing consumers to provide custom
state/context was to embed it as a field in Roadster's `AppContext`.
This is not how Axum recommends libraries support custom state, which
makes it difficult (impossible?) to integrate Roadster with other
Axum libraries.

The recommended approach is to keep the state generic, and add a type
constraint to require Roadster's state to be able to be retrieved from
the custom state using the
[FromRef](https://docs.rs/axum/latest/axum/extract/derive.FromRef.html)
trait. See the following for more details: https://docs.rs/axum/latest/axum/extract/struct.State.html#for-library-authors

One example of an integration that was blocked by our non-recommended
approach: Leptos requires the app's state provide Leptos's state using
`FromRef`. In order to support Leptos with our previous approach, we
would have needed to add the Leptos state directly to Roadster's
`AppContext`. This adds a dependency on Roadster for consumers who may
want to use Leptos, and also adds a direct dependency between Roadster
and Leptos (though, it would be optional behind a feature flag). This
maybe could work in the short term, but this approach doesn't scale to
other libraries -- Roadster would need to follow a similar approach for
all other libraries consumers may want to use. Instead, it's better to
simply follow Axum's recommendation to provide maximum flexibility to
consumers.
@spencewenski
spencewenski force-pushed the gh-249-custom-state-from-ref branch from 7b5d176 to 5513855 Compare June 30, 2024 00:37
@spencewenski spencewenski changed the title feat!: Use Axum's FromRef to allow custom state feat!: Switch to Axum's FromRef to allow custom state Jun 30, 2024
@spencewenski spencewenski changed the title feat!: Switch to Axum's FromRef to allow custom state feat!: Switch to Axum's FromRef for custom state Jun 30, 2024
@spencewenski
spencewenski marked this pull request as ready for review June 30, 2024 00:41
@spencewenski
spencewenski merged commit 65f5db4 into main Jun 30, 2024
@spencewenski
spencewenski deleted the gh-249-custom-state-from-ref branch June 30, 2024 00:42
This was referenced Jun 30, 2024
spencewenski pushed a commit that referenced this pull request Jul 1, 2024
## 🤖 New release
* `roadster`: 0.3.5 -> 0.4.0

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.4.0](roadster-v0.3.5...roadster-v0.4.0)
- 2024-07-01

### Added
- [**breaking**] Implement health check API using `HealthCheck` trait
([#255](#255))
- [**breaking**] Switch to Axum's `FromRef` for custom state
([#250](#250))

### Other
- [**breaking**] Remove deprecated items in preparation of 0.4 release
([#253](#253))
- Add example for integrating with Leptos
([#252](#252))
- Use small number of sidekiq workers for `full` example in dev/test
([#251](#251))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant