Skip to main content

PipeContext

Struct PipeContext 

Source
pub struct PipeContext { /* private fields */ }

Implementations§

Source§

impl PipeContext

Source

pub fn new() -> Self

Source

pub fn empty() -> Self

Source

pub fn clone_items(&self) -> Vec<PipeContextItem>

Source

pub fn use_handlers(&mut self)

Source

pub fn use_location_route( &mut self, url_path: impl Into<String>, loc_path: impl Into<String>, allow_symlink_escape: bool, )

Source

pub fn use_embedded_route( &mut self, url_path: impl Into<String>, assets: HashMap<String, Cow<'static, [u8]>>, )

Source

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,

Source

pub fn use_custom_sync<F>(&mut self, callback: F)
where F: Fn(&mut HttpRequest) -> Option<HttpResponse> + Send + Sync + 'static,

Source

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,

Source

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();
});
Source

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();
});
Source

pub fn use_limit_size(&mut self, max_header_bytes: usize, max_body_bytes: usize)

添加请求体大小限制中间件

§参数
  • max_header_bytes - Header 总大小限制 (字节)
  • max_body_bytes - Body 总大小限制 (字节)
§示例
let mut server = potato::HttpServer::new("127.0.0.1:8080");
server.configure(|ctx| {
    ctx.use_limit_size(1024 * 1024, 50 * 1024 * 1024); // 1MB header, 50MB body
    ctx.use_handlers();
});
Source

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();
});
Source

pub fn use_reverse_proxy( &mut self, url_path: impl Into<String>, proxy_url: impl Into<String>, modify_content: bool, )

Source

pub fn use_openapi(&mut self, url_path: impl Into<String>)

Source

pub async fn handle_request( self2: &PipeContext, req: &mut HttpRequest, skip: usize, ) -> HttpResponse

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
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