Service that accepts requests based on a MethodFilter and allows chaining additional handlers and services.
Whether or not MethodRouter implements Service depends on the state type it requires. A MethodRouter without required state (i.e., MethodRouter<()>) implements Service. If it requires state, you must call with_state() first.
Top-level handler functions
Route
GET requests to the given handler.Note that get routes will also be called for HEAD requests but will have the response body removed. Make sure to add explicit HEAD routes afterwards if needed.Route
POST requests to the given handler.Route
PUT requests to the given handler.Route
DELETE requests to the given handler.Route
PATCH requests to the given handler.Route
HEAD requests to the given handler.Route
OPTIONS requests to the given handler.Route
TRACE requests to the given handler.Route
CONNECT requests to the given handler.See MethodFilter::CONNECT for when you’d want to use this.Route requests with the given method filter to the handler.
Route requests with any method to the given handler.Additional methods can still be chained to override specific methods.
Top-level service functions
Route
GET requests to the given service.Note that get routes will also be called for HEAD requests but will have the response body removed.Route
POST requests to the given service.See get_service for an example.Route
PUT requests to the given service.See get_service for an example.Route
DELETE requests to the given service.See get_service for an example.Route
PATCH requests to the given service.See get_service for an example.Route
HEAD requests to the given service.See get_service for an example.Route
OPTIONS requests to the given service.See get_service for an example.Route
TRACE requests to the given service.See get_service for an example.Route
CONNECT requests to the given service.See MethodFilter::CONNECT for when you’d want to use this, and get_service for an example.Route requests with the given method to the service.
Route requests to the given service regardless of method.Additional methods can still be chained to override specific methods.
MethodRouter instance methods
Create a default
MethodRouter that will respond with 405 Method Not Allowed to all requests.Chain an additional handler that will accept requests matching the given
MethodFilter.Chain an additional handler that will only accept
GET requests.Note that get routes will also be called for HEAD requests but will have the response body removed.Chain an additional handler that will only accept
POST requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
PUT requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
DELETE requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
PATCH requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
HEAD requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
OPTIONS requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
TRACE requests.See MethodRouter::get for an example.Chain an additional handler that will only accept
CONNECT requests.See MethodRouter::get for an example.Chain an additional service that will accept requests matching the given
MethodFilter.Chain an additional service that will only accept
GET requests.Chain an additional service that will only accept
POST requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
PUT requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
DELETE requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
PATCH requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
HEAD requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
OPTIONS requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
TRACE requests.See MethodRouter::get_service for an example.Chain an additional service that will only accept
CONNECT requests.See MethodRouter::get_service for an example.Add a fallback handler to the method router.This handler is called when the request method doesn’t match any of the configured methods.
Add a fallback service to the method router.Like
fallback but accepts a Service instead of a handler.Apply a
tower::Layer to the method router.The layer is applied to all methods including the fallback.Apply a
tower::Layer to the method router’s routes, but not the fallback.This is useful when you want middleware to only apply to specific HTTP methods.Merge two method routers into one.This allows combining different method handlers. Both routers cannot have a custom fallback.
Provide the state for the method router.This converts a
MethodRouter<S> into a MethodRouter<()> that can be used as a service.Apply a
HandleErrorLayer.This is a convenience method for self.layer(HandleErrorLayer::new(f)).Get a
MethodFilter for the methods that this MethodRouter has custom code for.Returns None if the MethodRouter was constructed with any or has had a fallback set.Convert the method router into a
MakeService.This allows you to serve a single MethodRouter without path-based routing.Only available for MethodRouter<(), Infallible>.Convert the method router into a
MakeService which stores connection information.Requires the tokio feature. Only available for MethodRouter<(), Infallible>.