axum::routing::method_routing

Function any_service

Source
pub fn any_service<T, S>(svc: T) -> MethodRouter<S, T::Error>
where T: Service<Request> + Clone + Send + 'static, T::Response: IntoResponse + 'static, T::Future: Send + 'static, S: Clone,
Expand description

Route requests to the given service regardless of its method.

ยงExample

use axum::{
    extract::Request,
    Router,
    routing::any_service,
    body::Body,
};
use http::Response;
use std::convert::Infallible;

let service = tower::service_fn(|request: Request| 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::{
    extract::Request,
    Router,
    routing::any_service,
    body::Body,
};
use http::Response;
use std::convert::Infallible;

let service = tower::service_fn(|request: Request| async {
    // ...
});

let other_service = tower::service_fn(|request: Request| async {
    // ...
});

// `POST /` goes to `other_service`. All other requests go to `service`
let app = Router::new().route("/", any_service(service).post_service(other_service));