Skip to main content

lrwf_core/
middleware.rs

1//! Middleware trait: IMiddleware.
2
3use crate::error::Result;
4use crate::http::IHttpContext;
5
6/// Middleware component in the HTTP request pipeline.
7///
8/// Middlewares are called in registration order. Each middleware can:
9/// - Inspect/modify the request before passing through
10/// - Short-circuit by calling methods on the response without returning
11/// - The pipeline continues to the next middleware unless the response
12///   status has been set (short-circuit detection).
13///
14/// Analogous to ASP.NET Core's IMiddleware.
15#[async_trait::async_trait]
16pub trait IMiddleware: Send + Sync {
17    /// Process an HTTP request (before hook).
18    ///
19    /// Return `Ok(())` to continue the pipeline, or set the response
20    /// status to short-circuit further processing.
21    async fn invoke(&self, ctx: &mut dyn IHttpContext) -> Result<()>;
22
23    /// Called after the final handler has executed.
24    ///
25    /// Middleware post-processing, logging, response modification.
26    /// Default: no-op.
27    async fn after(&self, _ctx: &mut dyn IHttpContext) -> Result<()> {
28        Ok(())
29    }
30}