[−][src]Struct treemux::Router
Router dispatches requests to different handlers via configurable routes.
Fields
redirect_trailing_slash: bool
Enables automatic redirection if the current route can't be matched but a
handler for the path with (without) the trailing slash exists.
For example if /foo/
is requested but a route only exists for /foo
, the
client is redirected to /foo
with HTTP status code 301 for GET
requests
and 307 for all other request methods.
redirect_fixed_path: bool
If enabled, the router tries to fix the current request path, if no
handle is registered for it.
First superfluous path elements like ../
or //
are removed.
Afterwards the router does a case-insensitive lookup of the cleaned path.
If a handle can be found for this route, the router makes a redirection
to the corrected path with status code 301 for GET
requests and 307 for
all other request methods.
For example /FOO
and /..//Foo
could be redirected to /foo
.
redirect_trailing_slash
is independent of this option.
handle_options: bool
If enabled, the router automatically replies to OPTIONS
requests.
Custom OPTIONS
handlers take priority over automatic replies.
Implementations
impl Router
[src]
pub fn handle<P, H, R>(&mut self, path: P, method: Method, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Insert a value into the router for a specific path indexed by a key.
use treemux::Router; use hyper::{Response, Body, Method}; let mut router: Router = Router::default(); router.handle("/teapot", Method::GET, |_| { async { Ok(Response::new(Body::from("I am a teapot!"))) } });
pub fn lookup<P: AsRef<str>>(
&mut self,
method: &Method,
path: P
) -> Result<Match<'_, Route>, bool>
[src]
&mut self,
method: &Method,
path: P
) -> Result<Match<'_, Route>, bool>
Lookup allows the manual lookup of handler for a specific method and path.
If the handler is not found, it returns a Err(bool)
indicating whether a redirection should be performed to the same path with a trailing slash
use treemux::Router; use hyper::{Response, Body, Method}; let mut router = Router::default(); router.get("/home", |_| { async { Ok(Response::new(Body::from("Welcome!"))) } }); let res = router.lookup(&Method::GET, "/home").unwrap(); assert!(res.params.is_empty());
pub fn not_found<H, R>(&mut self, handler: H) where
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for when there is no match
pub fn method_not_allowed<H, R>(&mut self, handler: H) where
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for when the path matches a different method than the requested one
pub fn global_options<H, R>(&mut self, handler: H) where
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for when the path matches a different method than the requested one
pub fn get<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for GET
requests
pub fn head<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for HEAD
requests
pub fn options<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for OPTIONS
requests
pub fn post<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for POST
requests
pub fn put<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for PUT
requests
pub fn patch<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for PATCH
requests
pub fn delete<P, H, R>(&mut self, path: P, handler: H) where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<Body>>> + Send + 'static,
Register a handler for DELETE
requests
pub fn allowed<P: AsRef<str>>(&self, path: P) -> Vec<String>
[src]
Returns a list of the allowed methods for a specific path
use treemux::Router; use hyper::{Response, Body, Method}; let mut router = Router::default(); router.get("/home", |_| { async { Ok(Response::new(Body::from("Welcome!"))) } }); let allowed = router.allowed("/home"); assert!(allowed.contains(&"GET".to_string()));
impl Router
[src]
pub fn new() -> Self
[src]
pub fn into_service_with_context<T: Send + Sync + 'static>(
self,
context: T
) -> MakeRouterService<T>
[src]
self,
context: T
) -> MakeRouterService<T>
Converts the Router
into a Service
which you can serve directly with Hyper
.
If you have an existing Service
that you want to incorporate a Router
into, see
Router::serve
.
// Our router... let router = Router::default(); // Convert it into a service... let service = router.into_service(); // Serve with hyper hyper::Server::bind(&([127, 0, 0, 1], 3030).into()) .serve(service) .await?;
pub fn into_service(self) -> MakeRouterService<()>
[src]
Converts the Router
into a Service
which you can serve directly with Hyper
.
If you have an existing Service
that you want to incorporate a Router
into, see
Router::serve
.
// Our router... let router = Router::default(); // Convert it into a service... let service = router.into_service(); // Serve with hyper hyper::Server::bind(&([127, 0, 0, 1], 3030).into()) .serve(service) .await?;
pub async fn serve(&self, req: Request<Body>) -> Result<Response<Body>>
[src]
An asynchronous function from a Request
to a Response
. You will generally not need to use
this function directly, and instead use
Router::into_service
. However, it may be useful when
incorporating the router into a larger service.
let mut router: Router = Router::default(); let router = Arc::new(router); let make_svc = make_service_fn(move |_| { let router = router.clone(); async move { Ok::<_, Infallible>(service_fn(move |req: Request<Body>| { let router = router.clone(); async move { router.serve(req).await } })) } }); let server = Server::bind(&([127, 0, 0, 1], 3000).into()) .serve(make_svc) .await;
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Router
[src]
impl Send for Router
[src]
impl Sync for Router
[src]
impl Unpin for Router
[src]
impl !UnwindSafe for Router
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,