Struct sfo_http::http_server::Route
source · pub struct Route<'a, State> { /* private fields */ }Expand description
A handle to a route.
All HTTP requests are made against resources. After using Server::at (or
Route::at) to establish a route, the Route type can be used to
establish endpoints for various HTTP methods at that path. Also, using
nest, it can be used to set up a subrouter.
Implementations§
source§impl<'a, State> Route<'a, State>where
State: Clone + Send + Sync + 'static,
impl<'a, State> Route<'a, State>where State: Clone + Send + Sync + 'static,
sourcepub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State>
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State>
Extend the route with the given path.
sourcepub fn with<M>(&mut self, middleware: M) -> &mut Route<'a, State>where
M: Middleware<State>,
pub fn with<M>(&mut self, middleware: M) -> &mut Route<'a, State>where M: Middleware<State>,
Apply the given middleware to the current route.
sourcepub fn reset_middleware(&mut self) -> &mut Route<'a, State>
pub fn reset_middleware(&mut self) -> &mut Route<'a, State>
Reset the middleware chain for the current route, if any.
sourcepub fn nest<InnerState>(
&mut self,
service: Server<InnerState>
) -> &mut Route<'a, State>where
State: Clone + Send + Sync + 'static,
InnerState: Clone + Send + Sync + 'static,
pub fn nest<InnerState>( &mut self, service: Server<InnerState> ) -> &mut Route<'a, State>where State: Clone + Send + Sync + 'static, InnerState: Clone + Send + Sync + 'static,
Nest a Server at the current path.
sourcepub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<(), Error>
pub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<(), Error>
Serve a directory statically.
Each file will be streamed from disk, and a mime type will be determined based on magic bytes.
Examples
Serve the contents of the local directory ./public/images/* from
localhost:8080/images/*.
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/images").serve_dir("public/images/")?;
app.listen("127.0.0.1:8080").await?;
Ok(())
}sourcepub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<(), Error>
pub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<(), Error>
Serve a static file.
The file will be streamed from disk, and a mime type will be determined based on magic bytes. Similar to serve_dir
sourcepub fn method(
&mut self,
method: Method,
ep: impl Endpoint<State>
) -> &mut Route<'a, State>
pub fn method( &mut self, method: Method, ep: impl Endpoint<State> ) -> &mut Route<'a, State>
Add an endpoint for the given HTTP method
sourcepub fn all(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn all(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for all HTTP methods, as a fallback.
Routes with specific HTTP methods will be tried first.
sourcepub fn get(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn get(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for GET requests
sourcepub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for HEAD requests
sourcepub fn put(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn put(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for PUT requests
sourcepub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for POST requests
sourcepub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for DELETE requests
sourcepub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for OPTIONS requests
sourcepub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
pub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Route<'a, State>
Add an endpoint for CONNECT requests