pub struct PipeContext { /* private fields */ }Implementations§
Source§impl PipeContext
impl PipeContext
pub fn new() -> Self
pub fn empty() -> Self
pub fn clone_items(&self) -> Vec<PipeContextItem>
pub fn use_handlers(&mut self)
pub fn use_location_route( &mut self, url_path: impl Into<String>, loc_path: impl Into<String>, allow_symlink_escape: bool, )
pub fn use_embedded_route( &mut self, url_path: impl Into<String>, assets: HashMap<String, Cow<'static, [u8]>>, )
pub fn use_custom<F, Fut>(&mut self, callback: F)where
F: Fn(&mut HttpRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Option<HttpResponse>> + Send + 'static,
pub fn use_custom_sync<F>(&mut self, callback: F)
pub fn use_custom_async<F>(&mut self, callback: F)where
F: for<'a> Fn(&'a mut HttpRequest) -> Pin<Box<dyn Future<Output = Option<HttpResponse>> + Send + 'a>> + Send + Sync + 'static,
Sourcepub fn use_preprocess(
&mut self,
handler: for<'a> fn(&'a mut HttpRequest) -> Pin<Box<dyn Future<Output = Result<Option<HttpResponse>>> + Send + 'a>>,
)
pub fn use_preprocess( &mut self, handler: for<'a> fn(&'a mut HttpRequest) -> Pin<Box<dyn Future<Output = Result<Option<HttpResponse>>> + Send + 'a>>, )
添加全局预处理函数
预处理函数在所有路由处理之前执行,可以用于认证检查、日志记录等。
如果返回 Some(response),则直接返回该响应,跳过后续所有处理。
§参数
handler- 通过#[potato::preprocess]宏标注的预处理函数
§示例
ⓘ
#[potato::preprocess]
async fn my_preprocess(req: &mut HttpRequest) -> Option<HttpResponse> {
// 预处理逻辑
None
}
server.configure(|ctx| {
ctx.use_preprocess(my_preprocess);
ctx.use_handlers();
});Sourcepub fn use_postprocess(
&mut self,
handler: for<'a> fn(&'a mut HttpRequest, &'a mut HttpResponse) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>,
)
pub fn use_postprocess( &mut self, handler: for<'a> fn(&'a mut HttpRequest, &'a mut HttpResponse) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>, )
添加全局后处理函数
后处理函数在 handler 生成响应后执行,可以修改响应内容(如添加响应头)。
§参数
handler- 通过#[potato::postprocess]宏标注的后处理函数
§示例
ⓘ
#[potato::postprocess]
async fn my_postprocess(req: &mut HttpRequest, res: &mut HttpResponse) {
res.add_header("X-Custom".into(), "value".into());
}
server.configure(|ctx| {
ctx.use_postprocess(my_postprocess);
ctx.use_handlers();
});Sourcepub fn use_limit_size(&mut self, max_header_bytes: usize, max_body_bytes: usize)
pub fn use_limit_size(&mut self, max_header_bytes: usize, max_body_bytes: usize)
Sourcepub fn use_transfer_limit(
&mut self,
inbound_rate_bits_per_sec: u64,
outbound_rate_bits_per_sec: u64,
)
pub fn use_transfer_limit( &mut self, inbound_rate_bits_per_sec: u64, outbound_rate_bits_per_sec: u64, )
添加传输速率限制中间件
§参数
inbound_rate_bits_per_sec- 入站最大传输速率(bits/sec),接收请求数据的速率限制outbound_rate_bits_per_sec- 出站最大传输速率(bits/sec),发送响应数据的速率限制
§示例
let mut server = potato::HttpServer::new("127.0.0.1:8080");
server.configure(|ctx| {
ctx.use_transfer_limit(10_000_000, 20_000_000); // 入站 10 Mbps,出站 20 Mbps
ctx.use_handlers();
});pub fn use_reverse_proxy( &mut self, url_path: impl Into<String>, proxy_url: impl Into<String>, modify_content: bool, )
pub fn use_openapi(&mut self, url_path: impl Into<String>)
pub async fn handle_request( self2: &PipeContext, req: &mut HttpRequest, skip: usize, ) -> HttpResponse
Auto Trait Implementations§
impl Freeze for PipeContext
impl !RefUnwindSafe for PipeContext
impl Send for PipeContext
impl Sync for PipeContext
impl Unpin for PipeContext
impl UnsafeUnpin for PipeContext
impl !UnwindSafe for PipeContext
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