Skip to main content

MiddlewareBuilder

Struct MiddlewareBuilder 

Source
pub struct MiddlewareBuilder { /* private fields */ }
Expand description

中间件链构建器

持有 MiddlewareChain(顺序定义)+ 5 个 Option<Config>(各中间件配置), 通过 apply() 方法一次性应用到 axum::Router

§字段说明

字段类型说明
chainMiddlewareChain中间件顺序定义(业务期望顺序,首元素最先执行)
corsOption<CorsLayer>CORS Layer(基于 tower-http::cors,可直接应用)
logOption<LogConfig>Log 中间件配置
authOption<AuthConfig>Auth 中间件配置
rate_limitOption<RateLimitConfig>RateLimit 中间件配置
traceOption<TraceConfig>Trace 中间件配置

Implementations§

Source§

impl MiddlewareBuilder

Source

pub fn new() -> Self

创建空构建器(无中间件,无 Config)

Source

pub fn default_builder() -> Self

创建默认构建器(使用 DEFAULT_ORDER,但所有 Config 为 None

调用方需通过 with_xxx() 方法显式设置 Config,否则 apply() 会跳过该中间件。

Source

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() 覆盖。

Source

pub fn with_chain(self, chain: MiddlewareChain) -> Self

设置中间件链(替换现有链)

Source

pub fn with_cors(self, layer: CorsLayer) -> Self

设置 CORS Layer

Source

pub fn with_log(self, config: LogConfig) -> Self

设置 Log 配置

Source

pub fn with_auth(self, config: AuthConfig) -> Self

设置 Auth 配置

Source

pub fn with_rate_limit(self, config: RateLimitConfig) -> Self

设置 RateLimit 配置

Source

pub fn with_trace(self, config: TraceConfig) -> Self

设置 Trace 配置

Source

pub fn remove_kind(&mut self, kind: MiddlewareKind) -> usize

从链中移除所有指定类型的中间件(同时清除对应 Config)

返回被移除的中间件数量(仅链中数量,不包括 Config)。

Source

pub fn remove_from(&mut self, kind: MiddlewareKind) -> usize

从链中移除指定类型及之后的所有中间件(含指定类型)

用于「公开 API 跳过 Auth 及之后中间件」场景。 返回被移除的中间件数量;若 kind 不存在则不移除任何中间件,返回 0。

Source

pub fn chain(&self) -> &MiddlewareChain

返回中间件链引用

Source

pub fn cors(&self) -> Option<&CorsLayer>

返回 CORS Layer 引用

Source

pub fn log(&self) -> Option<&LogConfig>

返回 Log 配置引用

Source

pub fn auth(&self) -> Option<&AuthConfig>

返回 Auth 配置引用

Source

pub fn rate_limit(&self) -> Option<&RateLimitConfig>

返回 RateLimit 配置引用

Source

pub fn trace(&self) -> Option<&TraceConfig>

返回 Trace 配置引用

Source

pub fn is_enabled(&self, kind: MiddlewareKind) -> bool

判断指定中间件是否已启用(链中包含且 Config 已设置)

注意:Cors 的 Config 是 CorsLayer(必为 Some 才视为已启用)。

Source

pub fn apply(self, router: Router) -> Router

应用所有中间件到 axum::Router

chain.service_builder_order()(业务期望顺序的逆序)遍历调用 Router::layer, 保证业务期望顺序(chain.order())与实际执行顺序一致(Router::layer 后注册先执行)。

链中包含但 Config 未设置的中间件会被跳过(不应用)。

§消耗语义

此方法消耗 self(取出 Config 的所有权),返回应用了中间件的 Router

Trait Implementations§

Source§

impl Clone for MiddlewareBuilder

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for MiddlewareBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MiddlewareBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for MiddlewareBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

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§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more