For my CMS/Webapp framework project I'm using axum to handle the requests and responses. I have a App struct to contain the application state and some logic and a Plugin trait:
struct MyPlugin;
impl Plugin for MyPlugin {
fn initialize(&self, app: App) {
// do things like add routes
}
}
// register it to be run on startup
app.register_plugin(MyPlugin);
With this there are two main problems I need to solve:
There are two approaches to state: using Router.with_state and Extension. Is using Extension the better method?
Allowing use of the Router: its methods being self-consuming is tricky, would it be best to provide methods in App and then build it after plugins are run?
State is used for global state shared across requests, whereas extensions contain request-local data, like data retrieved from an access token, for example.
This is a really unhelpful response—I asked specifically about axum since it fits the needs of my project. I fail to see how the code you provided could possibly be integrated into the project, and I suspect it came from an LLM. You also called me 'he', which considering I am nonbinary, is incorrect.
Multiple State objects is tricky but possible, as long as you can split your routes and state in a way that each route only needs one State extractor. I'm not sure how you envision your API, but alternatively Axum also allows extracting "substate", if the substate type implements FromRef<State>. So if you can't split routes and state in a way that satisfies S in Router<S>, maybe you could make the substate-approach work instead. If that doesn't work or leads to an inelegant API, inserting the different states as a Extensions instead of using State would be the better option after all, in my opinion.
As far as I can tell the substate method wouldn't be extensible so I'll go with Extensions. I am thinking maybe I should have an API for registering Routers similar to the one for Plugins, then having code in App to merge them. Does this seem like a good approach?