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>
impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State>
sourcepub fn with<M>(&mut self, middleware: M) -> &mut Selfwhere
M: Middleware<State>,
pub fn with<M>(&mut self, middleware: M) -> &mut Selfwhere
M: Middleware<State>,
将给定中间件作为当前路由。
sourcepub fn reset_middleware(&mut self) -> &mut Self
pub fn reset_middleware(&mut self) -> &mut Self
重置当前路由的中间件
sourcepub fn nest<InnerState>(&mut self, service: Server<InnerState>) -> &mut Self
pub fn nest<InnerState>(&mut self, service: Server<InnerState>) -> &mut Self
在当前路径上嵌套 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(())
}sourcepub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<()>
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(())
}sourcepub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<()>
pub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<()>
提供静态文件。
每一个文件都将从磁盘io流传输,并确定了mime类型 基于magic bytes。类似serve_dir
sourcepub fn method(&mut self, method: Method, ep: impl Endpoint<State>) -> &mut Self
pub fn method(&mut self, method: Method, ep: impl Endpoint<State>) -> &mut Self
给定HTTP方法添加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> 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