Trait gotham::prelude::DrawRoutes
source · [−]pub trait DrawRoutes<C, P> where
C: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P: RefUnwindSafe + Send + Sync + 'static, {
Show 14 methods
fn get_or_head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn get<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn post<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn put<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn patch<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn delete<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn options<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor> { ... }
fn request<'b, IRM, M>(
&'b mut self,
matcher: IRM,
path: &str
) -> SingleRouteBuilder<'b, M, C, P, NoopPathExtractor, NoopQueryStringExtractor>
where
IRM: IntoRouteMatcher<Output = M>,
M: RouteMatcher + Send + Sync + 'static,
{ ... }
fn scope<F>(&mut self, path: &str, f: F)
where
F: FnOnce(&mut ScopeBuilder<'_, C, P>),
{ ... }
fn with_pipeline_chain<F, NC>(&mut self, pipeline_chain: NC, f: F)
where
F: FnOnce(&mut ScopeBuilder<'_, NC, P>),
NC: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
{ ... }
fn delegate<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, C, P> { ... }
fn delegate_without_pipelines<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, (), P> { ... }
fn associate<'b, F>(&'b mut self, path: &str, f: F)
where
F: FnOnce(&mut AssociatedRouteBuilder<'b, AnyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>),
{ ... }
}
Expand description
Defines functions used by a builder to determine which request paths will be dispatched to a
route. This trait is implemented by the top-level RouterBuilder
, and also the ScopedBuilder
created by DrawRoutes::scope
.
Provided methods
fn get_or_head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn get_or_head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches GET
and HEAD
requests to the given path.
Examples
build_simple_router(|route| {
route.get_or_head("/request/path").to(my_handler);
})
fn get<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn get<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches only GET
requests to the given path (ignoring HEAD
requests).
Examples
build_simple_router(|route| {
route.get("/request/path").to(my_handler);
})
fn head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn head<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches HEAD
requests to the given path.
Examples
build_simple_router(|route| {
route.head("/request/path").to(my_handler);
})
fn post<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn post<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches POST
requests to the given path.
Examples
build_simple_router(|route| {
route.post("/request/path").to(my_handler);
})
fn put<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn put<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches PUT
requests to the given path.
Examples
build_simple_router(|route| {
route.put("/request/path").to(my_handler);
})
fn patch<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn patch<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches PATCH
requests to the given path.
Examples
build_simple_router(|route| {
route.patch("/request/path").to(my_handler);
})
fn delete<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn delete<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches DELETE
requests to the given path.
Examples
build_simple_router(|route| {
route.delete("/request/path").to(my_handler);
})
fn options<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
fn options<'b>(
&'b mut self,
path: &str
) -> SingleRouteBuilder<'b, MethodOnlyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>
Creates a route which matches OPTIONS
requests to the given path.
Examples
build_simple_router(|route| {
route.options("/request/path").to(my_handler);
})
fn request<'b, IRM, M>(
&'b mut self,
matcher: IRM,
path: &str
) -> SingleRouteBuilder<'b, M, C, P, NoopPathExtractor, NoopQueryStringExtractor> where
IRM: IntoRouteMatcher<Output = M>,
M: RouteMatcher + Send + Sync + 'static,
fn request<'b, IRM, M>(
&'b mut self,
matcher: IRM,
path: &str
) -> SingleRouteBuilder<'b, M, C, P, NoopPathExtractor, NoopQueryStringExtractor> where
IRM: IntoRouteMatcher<Output = M>,
M: RouteMatcher + Send + Sync + 'static,
Creates a single route which matches any requests to the given path
with one of the
given methods
. The path
can consist of static or dynamic segments, for example:
"/hello/world"
- a static path, matching only a request for exactly"/hello/world"
"/hello/:name"
- a dynamic path, matching requests for"/hello/any_value_here"
Examples
build_simple_router(|route| {
route.request(vec![Method::GET, Method::HEAD], "/request/path").to(my_handler);
})
build_simple_router(|route| {
// All we match on is the Accept header, the method is not considered.
let matcher = AcceptHeaderRouteMatcher::new(vec![mime::APPLICATION_JSON]);
route.request(matcher, "/request/path").to(my_handler);
})
fn scope<F>(&mut self, path: &str, f: F) where
F: FnOnce(&mut ScopeBuilder<'_, C, P>),
fn scope<F>(&mut self, path: &str, f: F) where
F: FnOnce(&mut ScopeBuilder<'_, C, P>),
Begins defining a new scope, based on a given path
prefix.
Examples
build_simple_router(|route| {
route.scope("/api", |route| {
// Match requests to `/api/list`
route.get("/list").to(api::list);
});
})
fn with_pipeline_chain<F, NC>(&mut self, pipeline_chain: NC, f: F) where
F: FnOnce(&mut ScopeBuilder<'_, NC, P>),
NC: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
fn with_pipeline_chain<F, NC>(&mut self, pipeline_chain: NC, f: F) where
F: FnOnce(&mut ScopeBuilder<'_, NC, P>),
NC: PipelineHandleChain<P> + Copy + Send + Sync + 'static,
Begins a new scope at the current location, with an alternate pipeline chain.
Examples
let pipelines = new_pipeline_set();
let (pipelines, default) = pipelines.add(
new_pipeline()
.add(NewSessionMiddleware::default().with_session_type::<Session>())
.build()
);
let (pipelines, extended) = pipelines.add(
new_pipeline()
.add(NewSessionMiddleware::default().with_session_type::<AdminSession>())
.build()
);
let pipeline_set = finalize_pipeline_set(pipelines);
let default_chain = (default, ());
let extended_chain = (extended, default_chain);
build_router(default_chain, pipeline_set, |route| {
// Requests for the root handler use an empty set of pipelines, skipping the session
// middlewares.
route.with_pipeline_chain((), |route| {
route.get("/").to(handler);
});
// Requests dispatched to the resource module will only invoke one session
// middleware which is the default behavior.
route.get("/resource/list").to(resource::list);
// Requests for the admin handler will additionally invoke the admin session
// middleware.
route.with_pipeline_chain(extended_chain, |route| {
route.get("/admin").to(admin::handler);
});
})
fn delegate<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, C, P>
fn delegate<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, C, P>
Begins delegating a subpath of the tree.
Examples
fn admin_router() -> Router {
// Implementation elided
}
build_simple_router(|route| {
route.delegate("/admin").to_router(admin_router());
})
fn delegate_without_pipelines<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, (), P>
fn delegate_without_pipelines<'b>(
&'b mut self,
path: &str
) -> DelegateRouteBuilder<'b, AnyRouteMatcher, (), P>
Begins delegating a subpath of the tree, but does not dispatch the requests via this
router’s PipelineChain
.
Examples
// API routes which don't require sessions.
fn api_router() -> Router {
// Implementation elided
}
let (chain, pipelines) = single_pipeline(
new_pipeline()
.add(NewSessionMiddleware::default().with_session_type::<Session>())
.build()
);
build_router(chain, pipelines, |route| {
// Requests dispatched to the `/api` router will not invoke the session middleware.
route.delegate_without_pipelines("/api").to_router(api_router());
// Other requests will invoke the session middleware as normal.
route.get("/").to(handler);
})
fn associate<'b, F>(&'b mut self, path: &str, f: F) where
F: FnOnce(&mut AssociatedRouteBuilder<'b, AnyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>),
fn associate<'b, F>(&'b mut self, path: &str, f: F) where
F: FnOnce(&mut AssociatedRouteBuilder<'b, AnyRouteMatcher, C, P, NoopPathExtractor, NoopQueryStringExtractor>),
Begins associating routes with a fixed path in the tree. In this way, multiple routes can be quickly associated with a single location.
Examples
mod resource {
pub fn show(state: State) -> (State, Response<Body>) {
// Implementation elided.
}
pub fn update(state: State) -> (State, Response<Body>) {
// Implementation elided.
}
pub fn delete(state: State) -> (State, Response<Body>) {
// Implementation elided.
}
}
build_simple_router(|route| {
route.associate("/resource", |assoc| {
assoc.get_or_head().to(resource::show);
assoc.patch().to(resource::update);
assoc.delete().to(resource::delete);
});
})