958060e538d0c87a42e05b0ac69d5437cec0147c
axum-app-wrapper
A small plugin layer for axum applications, inspired by fastify from the Node/JS ecosystem.
Plugins can:
- add startup state before the final axum state type exists
- add routes, services, and middleware after state is initialized
- run graceful shutdown work in reverse registration order
This crate is intentionally thin. It does not replace axum's router, extractors, or middleware. Rather, it organizes your server setup and teardown into plugins.
Lifecycle
App::init() runs plugins in this order:
on_initin registration order, passing aTypeMapto build state.S::try_from(TypeMap)to build the final typed app state.on_setupin registration order, passingRouter<S>and&S.on_shutdownconsecutively in reverse registration order: the last registered plugin shuts down first, and each hook finishes before the next one starts.
Example
See the examples folder for full examples.
For on_setup closures that access typed state, construct the plugin as
AdHocPlugin::<AppState>::new(). That gives Rust enough context to infer the state parameter:
let plugin = AdHocPlugin::<AppState>::new()
.on_setup(|router, state| {
let config = Arc::clone(&state.config);
Ok(router.layer(axum::Extension(config)))
});
Reusable Plugins
Implement AppPlugin<S> directly when setup should be reusable across apps:
use axum::Router;
use axum_app_wrapper::{AppPlugin, TypeMap};
use futures::{future::BoxFuture, FutureExt};
struct ConfigPlugin {
config: Config,
}
impl AppPlugin<AppState> for ConfigPlugin {
fn on_init(&mut self, mut state: TypeMap) -> BoxFuture<'static, anyhow::Result<TypeMap>> {
let config = self.config.clone();
async move {
state.insert(config);
Ok(state)
}
.boxed()
}
fn on_setup(
&mut self,
router: Router<AppState>,
_state: &AppState,
) -> anyhow::Result<Router<AppState>> {
Ok(router.route("/health", axum::routing::get(health)))
}
}
Description
Languages
Rust
100%