Function axum::routing::method_routing::any_service
source · pub fn any_service<T, S, B>(svc: T) -> MethodRouter<S, B, T::Error>
Expand description
Route requests to the given service regardless of its method.
§Example
use axum::{
http::Request,
Router,
routing::any_service,
};
use http::Response;
use std::convert::Infallible;
use hyper::Body;
let service = tower::service_fn(|request: Request<Body>| async {
Ok::<_, Infallible>(Response::new(Body::empty()))
});
// All requests to `/` will go to `service`.
let app = Router::new().route("/", any_service(service));
Additional methods can still be chained:
use axum::{
http::Request,
Router,
routing::any_service,
};
use http::Response;
use std::convert::Infallible;
use hyper::Body;
let service = tower::service_fn(|request: Request<Body>| async {
// ...
});
let other_service = tower::service_fn(|request: Request<Body>| async {
// ...
});
// `POST /` goes to `other_service`. All other requests go to `service`
let app = Router::new().route("/", any_service(service).post_service(other_service));