Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`, `ConnectionInfo`, and
- [x] Provide built-in extractors `Message<T>`, `ConnectionInfo`, and
`SharedState<T>` (lines 792-840). `SharedState<T>` 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.

Expand All @@ -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`:

Comment thread
leynos marked this conversation as resolved.
```rust
Expand Down
34 changes: 13 additions & 21 deletions docs/rust-binary-router-library-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,39 +889,31 @@ 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<ServiceResponse, wireframe::Error> {
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`
builder:

```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**:

Expand Down