diff --git a/docs/roadmap.md b/docs/roadmap.md index f23611e5..70f2c5bb 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -72,27 +72,27 @@ after formatting. Line numbers below refer to that file. ## 2. Middleware and Extractors -- [ ] Develop a minimal middleware system and extractor traits for payloads, +- [x] Develop a minimal middleware system and extractor traits for payloads, connection metadata, and shared state. - - [ ] Define `FromMessageRequest` for extractor types (lines 760-782). See + - [x] Define `FromMessageRequest` for extractor types (lines 760-782). See [`FromMessageRequest`][from-message-request] in [`src/extractor.rs`](../src/extractor.rs). - - [ ] Provide built-in extractors `Message`, `ConnectionInfo`, and + - [x] Provide built-in extractors `Message`, `ConnectionInfo`, and `SharedState` (lines 792-840). `SharedState` is defined in [`src/extractor.rs`](../src/extractor.rs#L54-L87). - - [ ] Support custom extractors implementing `FromMessageRequest` (lines + - [x] Support custom extractors implementing `FromMessageRequest` (lines 842-858). Refer again to [`src/extractor.rs`](../src/extractor.rs#L39-L52). - [x] Implement middleware using `Transform`/`Service` traits. - - [x] Implement `ServiceRequest` and `ServiceResponse` wrappers - (lines 866-899) and introduce a `Next` helper to build the - asynchronous call chain. Trait definitions live in + - [x] Implement `ServiceRequest` and `ServiceResponse` wrappers (lines + 866-899) and introduce a `Next` helper to build the asynchronous call chain. + Trait definitions live in [`src/middleware.rs`](../src/middleware.rs#L71-L84). - - [ ] Provide a `from_fn` helper for functional middleware. + - [x] Provide a `from_fn` helper for functional middleware. - [x] Add tests verifying middleware can modify requests and observe responses. @@ -101,7 +101,7 @@ after formatting. Line numbers below refer to that file. on responses (lines 900-919). See the [`wrap` method](../src/app.rs#L73-L84). - - [ ] Document common middleware use cases like logging and authentication + - [x] Document common middleware use cases like logging and authentication (lines 920-935). Include a logging example using `from_fn`: ```rust diff --git a/docs/rust-binary-router-library-design.md b/docs/rust-binary-router-library-design.md index f038ed6b..9dc74de6 100644 --- a/docs/rust-binary-router-library-design.md +++ b/docs/rust-binary-router-library-design.md @@ -889,24 +889,18 @@ classDiagram Next --> Middleware ``` -A simplified functional middleware approach, similar to -`actix_web::middleware::from_fn` 26, could also be provided for simpler use -cases: +A simplified functional middleware approach uses `from_fn` so middleware can be +written as an async function or closure: ```rust -use wireframe::middleware::{Next, ServiceRequest, ServiceResponse}; // Hypothetical types - -async fn logging_mw_fn( - req: ServiceRequest, // Represents an incoming message/context - next: Next // Call to proceed to the next middleware or handler -) -> Result { - println!("Received message: {:?}", req.message_type_id()); - let res = next.call(req).await?; // Call next service in chain - if let Some(response_info) = res.info() { - println!("Sending response: {:?}", response_info); - } +use wireframe::middleware::from_fn; + +let logging = from_fn(|req, next| async move { + tracing::info!("--> received: {:?}", req.frame()); + let mut res = next.call(req).await?; + tracing::info!("<-- sending: {:?}", res.frame()); Ok(res) -} +}); ``` - **Registration**: Middleware would be registered with the `WireframeApp` @@ -914,14 +908,12 @@ async fn logging_mw_fn( ```rust WireframeApp::new() - .wrap(LoggingMiddleware::new()) - .wrap(AuthMiddleware::new(/* config */)) - // For functional middleware: - //.wrap(wireframe::middleware::from_fn(logging_mw_fn)) + .wrap(from_fn(|req, next| async move { /* auth */ next.call(req).await })) + .wrap(logging) ``` - Middleware is typically executed in the reverse order of registration for - incoming messages and in the registration order for outgoing responses. + The last middleware registered (`logging` above) runs first on incoming + messages and last on outgoing responses. - **Use Cases**: