What is a Middleware in Feather?
masterA middleware is a component in Feather's request processing pipeline that processes HTTP requests and responses. It can inspect request data (headers, body, path), mutate the response, control the flow of execution, or access application state.
Middlewares are defined by the Middleware trait, which requires a handle method:
pub trait Middleware: Send + Sync {
fn handle(&self, request: &mut Request, response: &mut Response, ctx: &AppContext) -> Outcome;
}Control flow is managed via the Outcome type, which is a Result wrapping a MiddlewareResult. This allows using the ? operator for clean error propagation.
pub enum MiddlewareResult {
Next, // Continue to next middleware
NextRoute, // Skip to next route handler
End, // Stop Executing and Send the Request.
}
pub type Outcome = Result<MiddlewareResult, Box<dyn Error>>;