pub trait Middleware<Input, Output, State>: Send + Sync{
// Required method
fn name(&self) -> &str;
// Provided methods
fn before_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Option<Vec<Output>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
fn after_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
outputs: Vec<Output>,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Vec<Output>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait { ... }
}Expand description
中间件Trait
可以在输入处理前后执行额外逻辑,如日志、认证、限流等。
Required Methods§
Provided Methods§
Sourcefn before_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Option<Vec<Output>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn before_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Option<Vec<Output>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
输入预处理
在处理输入之前调用。返回 None 表示不拦截,继续处理。
返回 Some(outputs) 表示拦截输入,直接返回这些输出。
Sourcefn after_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
outputs: Vec<Output>,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Vec<Output>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn after_handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_input: &'life1 Input,
outputs: Vec<Output>,
_ctx: &'life2 SecretaryContext<State>,
) -> Pin<Box<dyn Future<Output = Vec<Output>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
输出后处理
在产生输出后调用。可以修改或过滤输出。