pub trait Middleware: Send + Sync {
// Required method
fn handle(
&self,
req: &EnrichedRequest,
next: &dyn Fn(&EnrichedRequest) -> Result<IpcResponse, IpcError>,
) -> Result<IpcResponse, IpcError>;
}Expand description
Trait for implementing middleware
Middleware provides a more flexible way to intercept and transform requests and responses with a chain-of-responsibility pattern.
§Example
ⓘ
use dioxus_ipc_bridge::prelude::*;
struct AuthMiddleware {
token: String,
}
impl Middleware for AuthMiddleware {
fn handle(
&self,
req: &EnrichedRequest,
next: &dyn Fn(&EnrichedRequest) -> Result<IpcResponse, IpcError>,
) -> Result<IpcResponse, IpcError> {
// Check auth token
if req.headers.get("Authorization") != Some(&self.token) {
return Err(IpcError::Unauthorized);
}
// Continue chain
next(req)
}
}Required Methods§
Sourcefn handle(
&self,
req: &EnrichedRequest,
next: &dyn Fn(&EnrichedRequest) -> Result<IpcResponse, IpcError>,
) -> Result<IpcResponse, IpcError>
fn handle( &self, req: &EnrichedRequest, next: &dyn Fn(&EnrichedRequest) -> Result<IpcResponse, IpcError>, ) -> Result<IpcResponse, IpcError>
Process a request through the middleware chain
§Arguments
req- The enriched request with parsed parametersnext- Function to call the next middleware or route handler