pub struct MiddlewareBuilder { /* private fields */ }Expand description
中间件链构建器
持有 MiddlewareChain(顺序定义)+ 5 个 Option<Config>(各中间件配置),
通过 apply() 方法一次性应用到 axum::Router。
§字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
chain | MiddlewareChain | 中间件顺序定义(业务期望顺序,首元素最先执行) |
cors | Option<CorsLayer> | CORS Layer(基于 tower-http::cors,可直接应用) |
log | Option<LogConfig> | Log 中间件配置 |
auth | Option<AuthConfig> | Auth 中间件配置 |
rate_limit | Option<RateLimitConfig> | RateLimit 中间件配置 |
trace | Option<TraceConfig> | Trace 中间件配置 |
Implementations§
Source§impl MiddlewareBuilder
impl MiddlewareBuilder
Sourcepub fn default_builder() -> Self
pub fn default_builder() -> Self
创建默认构建器(使用 DEFAULT_ORDER,但所有 Config 为 None)
调用方需通过 with_xxx() 方法显式设置 Config,否则 apply() 会跳过该中间件。
Sourcepub fn php_global_builder() -> Self
pub fn php_global_builder() -> Self
创建 PHP 全局构建器(使用 PHP_GLOBAL_ORDER,对齐 app/middleware.php)
包含 Trace + Cors 两个中间件,对齐 PHP app/middleware.php 返回的全局中间件顺序。
默认设置 cors 字段为 Some(cors_layer()),调用方可通过 with_cors() 覆盖。
Sourcepub fn with_chain(self, chain: MiddlewareChain) -> Self
pub fn with_chain(self, chain: MiddlewareChain) -> Self
设置中间件链(替换现有链)
Sourcepub fn with_auth(self, config: AuthConfig) -> Self
pub fn with_auth(self, config: AuthConfig) -> Self
设置 Auth 配置
Sourcepub fn with_rate_limit(self, config: RateLimitConfig) -> Self
pub fn with_rate_limit(self, config: RateLimitConfig) -> Self
设置 RateLimit 配置
Sourcepub fn with_trace(self, config: TraceConfig) -> Self
pub fn with_trace(self, config: TraceConfig) -> Self
设置 Trace 配置
Sourcepub fn remove_kind(&mut self, kind: MiddlewareKind) -> usize
pub fn remove_kind(&mut self, kind: MiddlewareKind) -> usize
从链中移除所有指定类型的中间件(同时清除对应 Config)
返回被移除的中间件数量(仅链中数量,不包括 Config)。
Sourcepub fn remove_from(&mut self, kind: MiddlewareKind) -> usize
pub fn remove_from(&mut self, kind: MiddlewareKind) -> usize
从链中移除指定类型及之后的所有中间件(含指定类型)
用于「公开 API 跳过 Auth 及之后中间件」场景。
返回被移除的中间件数量;若 kind 不存在则不移除任何中间件,返回 0。
Sourcepub fn chain(&self) -> &MiddlewareChain
pub fn chain(&self) -> &MiddlewareChain
返回中间件链引用
Sourcepub fn auth(&self) -> Option<&AuthConfig>
pub fn auth(&self) -> Option<&AuthConfig>
返回 Auth 配置引用
Sourcepub fn rate_limit(&self) -> Option<&RateLimitConfig>
pub fn rate_limit(&self) -> Option<&RateLimitConfig>
返回 RateLimit 配置引用
Sourcepub fn trace(&self) -> Option<&TraceConfig>
pub fn trace(&self) -> Option<&TraceConfig>
返回 Trace 配置引用
Sourcepub fn is_enabled(&self, kind: MiddlewareKind) -> bool
pub fn is_enabled(&self, kind: MiddlewareKind) -> bool
判断指定中间件是否已启用(链中包含且 Config 已设置)
注意:Cors 的 Config 是 CorsLayer(必为 Some 才视为已启用)。
Trait Implementations§
Source§impl Clone for MiddlewareBuilder
impl Clone for MiddlewareBuilder
Source§fn clone(&self) -> MiddlewareBuilder
fn clone(&self) -> MiddlewareBuilder
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MiddlewareBuilder
impl Debug for MiddlewareBuilder
Source§impl Default for MiddlewareBuilder
impl Default for MiddlewareBuilder
Auto Trait Implementations§
impl !Freeze for MiddlewareBuilder
impl !RefUnwindSafe for MiddlewareBuilder
impl !UnwindSafe for MiddlewareBuilder
impl Send for MiddlewareBuilder
impl Sync for MiddlewareBuilder
impl Unpin for MiddlewareBuilder
impl UnsafeUnpin for MiddlewareBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.