rust_webx_core/middleware.rs
1//! Middleware trait: IMiddleware.
2
3use crate::error::Result;
4use crate::http::IHttpContext;
5use std::ops::ControlFlow;
6
7/// Middleware component in the HTTP request pipeline.
8///
9/// Middlewares are called in registration order. Each middleware can:
10/// - Inspect/modify the request before passing through
11/// - Short-circuit by returning `ControlFlow::Break(())`
12/// - The pipeline continues to the next middleware on `ControlFlow::Continue(())`
13///
14/// On short-circuit, remaining middleware and the final handler are skipped,
15/// but `after` hooks on already-executed middleware still run in reverse order.
16///
17/// Analogous to ASP.NET Core's IMiddleware.
18#[async_trait::async_trait]
19pub trait IMiddleware: Send + Sync {
20 /// Process an HTTP request (before hook).
21 ///
22 /// Return `Ok(ControlFlow::Continue(()))` to continue the pipeline,
23 /// or `Ok(ControlFlow::Break(()))` to short-circuit (skip remaining
24 /// middleware and the final handler; `after` hooks on already-executed
25 /// middleware still run in reverse order).
26 async fn invoke(&self, ctx: &mut dyn IHttpContext) -> Result<ControlFlow<()>>;
27
28 /// Called after the final handler has executed.
29 ///
30 /// Middleware post-processing, logging, response modification.
31 /// Default: no-op.
32 async fn after(&self, _ctx: &mut dyn IHttpContext) -> Result<()> {
33 Ok(())
34 }
35}