pub struct Treemux { /* private fields */ }
Implementations§
source§impl Treemux
impl Treemux
pub fn builder() -> Builder<Identity>
sourcepub fn lookup<'b, P: AsRef<str>>(
&'b self,
method: &'b Method,
path: P
) -> Result<(RequestHandler, Params), bool>
pub fn lookup<'b, P: AsRef<str>>( &'b self, method: &'b Method, path: P ) -> Result<(RequestHandler, Params), 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::{Treemux, RouterBuilder};
use hyper::{Response, Body, Method};
let mut router = Treemux::builder();
router.get("/home", |_| {
async { Ok(Response::new(Body::from("Welcome!"))) }
});
let router: Treemux = router.into();
let res = router.lookup(&Method::GET, "/home").unwrap();
assert!(res.1.is_empty());
sourcepub fn into_service_with_context<T: Send + Sync + 'static>(
self,
context: T
) -> MakeRouterService<T>
pub fn into_service_with_context<T: Send + Sync + 'static>( self, context: T ) -> MakeRouterService<T>
Converts the Treemux
into a Service
which you can serve directly with Hyper
.
If you have an existing Service
that you want to incorporate a Treemux
into, see
Treemux::serve
.
// Our router...
let router = Treemux::builder();
// 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?;
sourcepub fn into_service(self) -> MakeRouterService<()>
pub fn into_service(self) -> MakeRouterService<()>
Converts the Treemux
into a Service
which you can serve directly with Hyper
.
If you have an existing Service
that you want to incorporate a Treemux
into, see
Treemux::serve
.
// Our router...
let router = Treemux::builder();
// 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?;
sourcepub async fn serve(&self, req: Request<Body>) -> ResponseResult
pub async fn serve(&self, req: Request<Body>) -> ResponseResult
An asynchronous function from a Request
to a Response
. You will generally not need to use
this function directly, and instead use
Treemux::into_service
. However, it may be useful when
incorporating the router into a larger service.
let mut router = Treemux::builder();
let router: Arc<Treemux> = Arc::new(router.into());
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;