pub fn from_fn<F, Fut>(f: F) -> Arc<FnMiddleware<F>>Expand description
Helper function to create middleware from async functions.
This is a convenience function that wraps an async function in a middleware.
§Arguments
f- Async function with signature matching middleware requirements
§Examples
§Simple Logging
use wsforge::prelude::*;
let logging = from_fn(|msg, conn, state, ext, mut next| async move {
println!("Processing message from {}", conn.id());
next.run(msg, conn, state, ext).await
});§With State Access
use wsforge::prelude::*;
use std::sync::Arc;
let counter = from_fn(|msg, conn, state, ext, mut next| async move {
// Access state
if let Some(counter) = state.get::<Arc<std::sync::atomic::AtomicU64>>() {
counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
next.run(msg, conn, state, ext).await
});