Struct summer_boot::Route

source ·
pub struct Route<'a, State> { /* private fields */ }
Expand description

A handle to route

所有HTTP请求都是针对资源请求的。 使用Server::at 或者 Route::at 创建路由,可以使用 Route 类型 为路径的一些HTTP方法创建endpoints

Implementations§

source§

impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State>

source

pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State>

使用指定 path 添加路由。

source

pub fn path(&self) -> &str

获取当前路径

source

pub fn with<M>(&mut self, middleware: M) -> &mut Self
where M: Middleware<State>,

将给定中间件作为当前路由。

source

pub fn reset_middleware(&mut self) -> &mut Self

重置当前路由的中间件

source

pub fn nest<InnerState>(&mut self, service: Server<InnerState>) -> &mut Self
where State: Clone + Send + Sync + 'static, InnerState: Clone + Send + Sync + 'static,

在当前路径上嵌套 Server

Note

其他服务 始终 具有优先权 重叠路径,这个例子输入 /hello 将 返回 “Unexpected” 给客户端

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
    let mut app = summer_boot::new();
    app.at("/hello").nest({
        let mut example = summer_boot::with_state("world");
        example
            .at("/")
            .get(|req: summer_boot::Request<&'static str>| async move {
                Ok(format!("Hello {state}!", state = req.state()))
            });
        example
    });
    app.at("/*").get(|_| async { Ok("Unexpected") });
    app.listen("127.0.0.1:8080").await?;
    Ok(())
}
source

pub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<()>

静态目录服务。

每一个文件都将从磁盘io流传输,并确定了mime类型

Security

这个方法确保了除了指定文件夹下之外的文件的路径 无论是否存在都会返回StatusCode::Forbidden

Examples

本地服务提供目录 ./public/images/* 来自路径 localhost:8080/images/*.

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
    let mut app = summer_boot::new();
    // app.at("/images/*").serve_dir("public/images/")?;
    app.listen("127.0.0.1:8080").await.unwrap();
    Ok(())
}
source

pub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<()>

提供静态文件。

每一个文件都将从磁盘io流传输,并确定了mime类型 基于magic bytes。类似serve_dir

source

pub fn method(&mut self, method: Method, ep: impl Endpoint<State>) -> &mut Self

给定HTTP方法添加endpoint

source

pub fn all(&mut self, ep: impl Endpoint<State>) -> &mut Self

为所有HTTP方法添加一个endpoin,作为回调。

尝试使用特定HTTP方法的路由。

source

pub fn get(&mut self, ep: impl Endpoint<State>) -> &mut Self

GET 请求添加endpoint

source

pub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Self

HEAD 请求添加endpoint

source

pub fn put(&mut self, ep: impl Endpoint<State>) -> &mut Self

PUT 请求添加endpoint

source

pub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Self

POST 请求添加endpoint

source

pub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Self

为 `DELETE 请求添加endpoint

source

pub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Self

OPTIONS 请求添加endpoint

source

pub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Self

CONNECT 请求添加endpoint

source

pub fn patch(&mut self, ep: impl Endpoint<State>) -> &mut Self

PATCH 请求添加endpoint

source

pub fn trace(&mut self, ep: impl Endpoint<State>) -> &mut Self

TRACE 请求添加endpoint

Auto Trait Implementations§

§

impl<'a, State> !RefUnwindSafe for Route<'a, State>

§

impl<'a, State> Send for Route<'a, State>

§

impl<'a, State> Sync for Route<'a, State>

§

impl<'a, State> Unpin for Route<'a, State>

§

impl<'a, State> !UnwindSafe for Route<'a, State>

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.

§

impl<T> Instrument for T

§

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

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

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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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