Skip to content

Commit 04a9964

Browse files
committed
middleware/app: Add axum equivalent
1 parent ac3856a commit 04a9964

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/middleware/app.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use super::prelude::*;
2+
use axum::extract::State;
3+
use axum::middleware::Next;
4+
use axum::response::Response;
5+
use http::Request;
26

37
use crate::app::AppState;
48

@@ -20,6 +24,17 @@ impl Middleware for AppMiddleware {
2024
}
2125
}
2226

27+
/// `axum` middleware that injects the `AppState` instance into the `Request` extensions.
28+
pub async fn add_app_state_extension<B>(
29+
State(app_state): State<AppState>,
30+
mut request: Request<B>,
31+
next: Next<B>,
32+
) -> Response {
33+
request.extensions_mut().insert(app_state);
34+
35+
next.run(request).await
36+
}
37+
2338
/// Adds an `app()` method to the `Request` type returning the global `App` instance
2439
pub trait RequestApp {
2540
fn app(&self) -> &AppState;

0 commit comments

Comments
 (0)