pub struct MethodRouter<B = Body, E = Infallible> { /* private fields */ }server only.Expand description
A method router that handle the request and dispatch it by its method.
There is no need to create MethodRouter directly, you can use specific method for creating
it. What’s more, the method router allows chaining additional handlers or services.
§Examples
use std::convert::Infallible;
use volo::service::service_fn;
use volo_http::{
context::ServerContext,
request::Request,
server::route::{MethodRouter, Router, any, get, post_service},
};
async fn index() -> &'static str {
"Hello, World"
}
async fn index_fn(cx: &mut ServerContext, req: Request) -> Result<&'static str, Infallible> {
Ok("Hello, World")
}
let _: MethodRouter = get(index);
let _: MethodRouter = any(index);
let _: MethodRouter = post_service(service_fn(index_fn));
let _: MethodRouter = get(index).post(index).options_service(service_fn(index_fn));
let app: Router = Router::new().route("/", get(index));
let app: Router = Router::new().route("/", get(index).post(index).head(index));Implementations§
Source§impl<B, E> MethodRouter<B, E>where
B: Send + 'static,
E: 'static,
impl<B, E> MethodRouter<B, E>where
B: Send + 'static,
E: 'static,
Sourcepub fn layer<L, B2, E2>(self, l: L) -> MethodRouter<B2, E2>
pub fn layer<L, B2, E2>(self, l: L) -> MethodRouter<B2, E2>
Add a new inner layer to all routes in this method router.
The layer’s Service should be Clone + Send + Sync + 'static.
Source§impl<B, E> MethodRouter<B, E>where
B: Send + 'static,
E: IntoResponse + 'static,
impl<B, E> MethodRouter<B, E>where
B: Send + 'static,
E: IntoResponse + 'static,
Sourcepub fn options_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn options_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route options requests to the given service.
Sourcepub fn get_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn get_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route get requests to the given service.
Sourcepub fn post_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn post_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route post requests to the given service.
Sourcepub fn put_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn put_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route put requests to the given service.
Sourcepub fn delete_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn delete_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route delete requests to the given service.
Sourcepub fn head_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn head_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route head requests to the given service.
Sourcepub fn trace_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn trace_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route trace requests to the given service.
Sourcepub fn connect_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn connect_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route connect requests to the given service.
Sourcepub fn patch_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn patch_service<S>(self, service: S) -> MethodRouter<B, E>where
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Route patch requests to the given service.
Sourcepub fn fallback<H, T>(self, handler: H) -> Self
pub fn fallback<H, T>(self, handler: H) -> Self
Set a fallback handler for the route.
If there is no method that the route can handle, method router will call the fallback handler.
Default is returning “405 Method Not Allowed”.
Sourcepub fn fallback_service<S>(self, service: S) -> Selfwhere
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
pub fn fallback_service<S>(self, service: S) -> Selfwhere
for<'a> S: Service<ServerContext, Request<B>, Error = E> + Send + Sync + 'a,
S::Response: IntoResponse,
Set a fallback service for the route.
If there is no method that the route can handle, method router will call the fallback service.
Default is returning “405 Method Not Allowed”.