Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how state works with merge and nest #1313

Closed
davidpdrsn opened this issue Aug 24, 2022 · 5 comments
Closed

Document how state works with merge and nest #1313

davidpdrsn opened this issue Aug 24, 2022 · 5 comments
Assignees
Labels
A-axum T-docs Topic: documentation
Milestone

Comments

@davidpdrsn
Copy link
Member

Gotten a few questions about how states work with merge and nest. Things like whether the state needs to be passed to each router and whether the state of the inner routers is dropped. This is something we should document.

@davidpdrsn davidpdrsn added T-docs Topic: documentation A-axum labels Aug 24, 2022
@davidpdrsn davidpdrsn self-assigned this Aug 24, 2022
@davidpdrsn davidpdrsn added this to the 0.6 milestone Aug 24, 2022
@genusistimelord
Copy link
Contributor

Yeah I noticed this when using AxumFlash and passing the config as the State. I had to change my Routing functions to take in the State to set them individually.

Router::with_state(flash_config.clone())
        .route("/", get(root))
       .merge(main_routes(flash_config.clone()))

which results in

pub fn main_routes(config: axum_flash::Config) -> Router<axum_flash::Config> {
    Router::with_state(config)
        .route("/main", get(main_page))
}

@davidpdrsn
Copy link
Member Author

#1368 is making things more flexible. When that is in I'll write some docs for it.

@davidpdrsn
Copy link
Member Author

I think we're in a good place in terms for docs. We can expand things when people ask questions but for now I'll close this.

@jimmycuadra
Copy link
Contributor

jimmycuadra commented Nov 15, 2022

I think the docs could still be improved for this. I looked in these three places to try to figure this out (none of which explain it) before I found the API docs for Router::inherit_state:

  • The "Sharing state with handlers" section of the root API documentation page
  • API docs for axum::extract::State
  • The "Routing" section of the root API documentation page

I think it's worth explaining directly in the documentation for State or at the very least linking to Router::inherit_state from there.

@davidpdrsn
Copy link
Member Author

Good idea. Do you wanna make a pr?

jimmycuadra added a commit to jimmycuadra/axum that referenced this issue Nov 19, 2022
I was going to improve the discoverability of docs for how state works
with `Router::merge` and `Router::nest` based on my comment in tokio-rs#1313,
but tokio-rs#1532 removes `Router::inherit_state` so I discovered that the
change I originally suggested isn't necessary anymore.

I decided to make a few other minor documentation fixes while I was
reading, however:

* Corrected the docs for `Router::nest` to say that it nests another
  `Router` rather than a `Service`. There is a separate function
  (`Router::nest_service`) for the latter.
* Changed one of the headings in nest.md to use more idiomatic English.
* Changed awkward phrasing under the "Sharing state with handlers"
  heading of the root documentation page.
* Removed a trailing period from one of three list items for
  consistency.
jimmycuadra added a commit to jimmycuadra/axum that referenced this issue Nov 19, 2022
Per my comment in tokio-rs#1313, this improves discoverability of how `State`
works with nested/merged `Router`s.

I made a few other minor documentation fixes while I was reading:

* Corrected the docs for `Router::nest` to say that it nests another
  `Router` rather than a `Service`. There is a separate function
  (`Router::nest_service`) for the latter.
* Changed one of the headings in nest.md to use more idiomatic English.
* Changed awkward phrasing under the "Sharing state with handlers"
  heading of the root documentation page.
* Removed a trailing period from one of three list items for
  consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum T-docs Topic: documentation
Projects
None yet
Development

No branches or pull requests

3 participants