2021-10-24 22:05:16 +02:00
|
|
|
//! Routing for [`Service`'s] based on HTTP methods.
|
2021-06-08 12:43:16 +02:00
|
|
|
//!
|
|
|
|
|
//! Most of the time applications will be written by composing
|
|
|
|
|
//! [handlers](crate::handler), however sometimes you might have some general
|
|
|
|
|
//! [`Service`] that you want to route requests to. That is enabled by the
|
|
|
|
|
//! functions in this module.
|
|
|
|
|
//!
|
|
|
|
|
//! # Example
|
|
|
|
|
//!
|
|
|
|
|
//! Using [`Redirect`] to redirect requests can be done like so:
|
|
|
|
|
//!
|
|
|
|
|
//! ```
|
|
|
|
|
//! use tower_http::services::Redirect;
|
2021-08-18 00:04:15 +02:00
|
|
|
//! use axum::{
|
|
|
|
|
//! body::Body,
|
2021-11-01 22:13:37 +01:00
|
|
|
//! routing::{get, service_method_routing as service},
|
2021-08-18 00:04:15 +02:00
|
|
|
//! http::Request,
|
2021-08-19 22:37:48 +02:00
|
|
|
//! Router,
|
2021-08-18 00:04:15 +02:00
|
|
|
//! };
|
2021-06-08 12:43:16 +02:00
|
|
|
//!
|
|
|
|
|
//! async fn handler(request: Request<Body>) { /* ... */ }
|
|
|
|
|
//!
|
|
|
|
|
//! let redirect_service = Redirect::<Body>::permanent("/new".parse().unwrap());
|
|
|
|
|
//!
|
2021-08-19 22:37:48 +02:00
|
|
|
//! let app = Router::new()
|
|
|
|
|
//! .route("/old", service::get(redirect_service))
|
2021-08-18 00:04:15 +02:00
|
|
|
//! .route("/new", get(handler));
|
2021-06-08 12:43:16 +02:00
|
|
|
//! # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
//! # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-08 12:43:16 +02:00
|
|
|
//! # };
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2021-06-08 23:55:25 +02:00
|
|
|
//! # Regarding backpressure and `Service::poll_ready`
|
|
|
|
|
//!
|
|
|
|
|
//! Generally routing to one of multiple services and backpressure doesn't mix
|
|
|
|
|
//! well. Ideally you would want ensure a service is ready to receive a request
|
2021-08-01 11:55:09 -07:00
|
|
|
//! before calling it. However, in order to know which service to call, you need
|
|
|
|
|
//! the request...
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
|
|
|
|
//! One approach is to not consider the router service itself ready until all
|
|
|
|
|
//! destination services are ready. That is the approach used by
|
|
|
|
|
//! [`tower::steer::Steer`].
|
|
|
|
|
//!
|
|
|
|
|
//! Another approach is to always consider all services ready (always return
|
|
|
|
|
//! `Poll::Ready(Ok(()))`) from `Service::poll_ready` and then actually drive
|
2021-06-09 07:52:04 +02:00
|
|
|
//! readiness inside the response future returned by `Service::call`. This works
|
|
|
|
|
//! well when your services don't care about backpressure and are always ready
|
|
|
|
|
//! anyway.
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
2021-07-09 21:36:14 +02:00
|
|
|
//! axum expects that all services used in your app wont care about
|
2021-06-08 23:55:25 +02:00
|
|
|
//! backpressure and so it uses the latter strategy. However that means you
|
|
|
|
|
//! should avoid routing to a service (or using a middleware) that _does_ care
|
|
|
|
|
//! about backpressure. At the very least you should [load shed] so requests are
|
|
|
|
|
//! dropped quickly and don't keep piling up.
|
|
|
|
|
//!
|
2021-08-01 11:55:09 -07:00
|
|
|
//! It also means that if `poll_ready` returns an error then that error will be
|
|
|
|
|
//! returned in the response future from `call` and _not_ from `poll_ready`. In
|
|
|
|
|
//! that case, the underlying service will _not_ be discarded and will continue
|
|
|
|
|
//! to be used for future requests. Services that expect to be discarded if
|
2021-07-09 21:36:14 +02:00
|
|
|
//! `poll_ready` fails should _not_ be used with axum.
|
2021-06-09 07:52:04 +02:00
|
|
|
//!
|
2021-06-08 23:55:25 +02:00
|
|
|
//! One possible approach is to only apply backpressure sensitive middleware
|
2021-07-09 21:36:14 +02:00
|
|
|
//! around your entire app. This is possible because axum applications are
|
2021-06-08 23:55:25 +02:00
|
|
|
//! themselves services:
|
|
|
|
|
//!
|
|
|
|
|
//! ```rust
|
2021-08-18 00:04:15 +02:00
|
|
|
//! use axum::{
|
2021-10-24 22:05:16 +02:00
|
|
|
//! routing::get,
|
2021-08-19 22:37:48 +02:00
|
|
|
//! Router,
|
2021-08-18 00:04:15 +02:00
|
|
|
//! };
|
2021-06-08 23:55:25 +02:00
|
|
|
//! use tower::ServiceBuilder;
|
|
|
|
|
//! # let some_backpressure_sensitive_middleware =
|
|
|
|
|
//! # tower::layer::util::Identity::new();
|
|
|
|
|
//!
|
2021-06-09 09:03:09 +02:00
|
|
|
//! async fn handler() { /* ... */ }
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
2021-08-19 22:37:48 +02:00
|
|
|
//! let app = Router::new().route("/", get(handler));
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
|
|
|
|
//! let app = ServiceBuilder::new()
|
|
|
|
|
//! .layer(some_backpressure_sensitive_middleware)
|
|
|
|
|
//! .service(app);
|
2021-06-19 12:50:33 +02:00
|
|
|
//! # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
//! # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-19 12:50:33 +02:00
|
|
|
//! # };
|
2021-06-08 23:55:25 +02:00
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! However when applying middleware around your whole application in this way
|
2021-06-09 07:52:04 +02:00
|
|
|
//! you have to take care that errors are still being handled with
|
|
|
|
|
//! appropriately.
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
|
|
|
|
//! Also note that handlers created from async functions don't care about
|
|
|
|
|
//! backpressure and are always ready. So if you're not using any Tower
|
2021-06-09 07:52:04 +02:00
|
|
|
//! middleware you don't have to worry about any of this.
|
2021-06-08 23:55:25 +02:00
|
|
|
//!
|
2021-06-08 12:43:16 +02:00
|
|
|
//! [`Redirect`]: tower_http::services::Redirect
|
2021-06-08 23:55:25 +02:00
|
|
|
//! [load shed]: tower::load_shed
|
2021-10-24 22:05:16 +02:00
|
|
|
//! [`Service`'s]: tower::Service
|
2021-06-08 12:43:16 +02:00
|
|
|
|
|
|
|
|
use crate::{
|
2021-10-24 22:05:16 +02:00
|
|
|
body::{box_body, BoxBody},
|
2021-10-25 23:28:22 +02:00
|
|
|
routing::{MethodFilter, MethodNotAllowed},
|
2021-10-24 22:05:16 +02:00
|
|
|
util::{Either, EitherProj},
|
|
|
|
|
BoxError,
|
2021-06-08 12:43:16 +02:00
|
|
|
};
|
|
|
|
|
use bytes::Bytes;
|
2021-10-24 22:05:16 +02:00
|
|
|
use futures_util::ready;
|
|
|
|
|
use http::{Method, Request, Response};
|
|
|
|
|
use http_body::Empty;
|
|
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
|
use std::marker::PhantomData;
|
2021-06-08 12:43:16 +02:00
|
|
|
use std::{
|
2021-10-24 22:05:16 +02:00
|
|
|
future::Future,
|
|
|
|
|
pin::Pin,
|
2021-06-08 12:43:16 +02:00
|
|
|
task::{Context, Poll},
|
|
|
|
|
};
|
2021-10-24 22:05:16 +02:00
|
|
|
use tower::util::Oneshot;
|
2021-10-24 19:33:03 +02:00
|
|
|
use tower::ServiceExt as _;
|
2021-08-21 15:01:30 +02:00
|
|
|
use tower_service::Service;
|
2021-06-08 12:43:16 +02:00
|
|
|
|
2021-09-19 10:28:15 +02:00
|
|
|
/// Route requests with any standard HTTP method to the given service.
|
2021-06-13 10:10:37 +02:00
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-09-19 10:28:15 +02:00
|
|
|
///
|
|
|
|
|
/// Note that this only accepts the standard HTTP methods. If you need to
|
|
|
|
|
/// support non-standard methods you can route directly to a [`Service`].
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn any<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-13 10:10:37 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-13 10:10:37 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::all(), svc)
|
2021-06-13 10:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-08 12:43:16 +02:00
|
|
|
/// Route `DELETE` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn delete<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::DELETE, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `GET` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2021-08-18 00:04:15 +02:00
|
|
|
/// use axum::{
|
|
|
|
|
/// http::Request,
|
2021-08-19 22:37:48 +02:00
|
|
|
/// Router,
|
2021-11-01 22:13:37 +01:00
|
|
|
/// routing::service_method_routing as service,
|
2021-08-18 00:04:15 +02:00
|
|
|
/// };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// 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()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // Requests to `GET /` will go to `service`.
|
2021-08-19 22:37:48 +02:00
|
|
|
/// let app = Router::new().route("/", service::get(service));
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// ```
|
2021-08-15 20:27:13 +02:00
|
|
|
///
|
|
|
|
|
/// 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.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn get<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-15 20:27:13 +02:00
|
|
|
on(MethodFilter::GET | MethodFilter::HEAD, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `HEAD` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn head<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::HEAD, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `OPTIONS` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn options<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::OPTIONS, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `PATCH` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn patch<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::PATCH, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `POST` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn post<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::POST, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `PUT` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn put<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::PUT, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route `TRACE` requests to the given service.
|
|
|
|
|
///
|
|
|
|
|
/// See [`get`] for an example.
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn trace<S, B>(svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
on(MethodFilter::TRACE, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Route requests with the given method to the service.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2021-08-18 00:04:15 +02:00
|
|
|
/// use axum::{
|
|
|
|
|
/// http::Request,
|
2021-10-24 22:05:16 +02:00
|
|
|
/// routing::on,
|
2021-08-19 22:37:48 +02:00
|
|
|
/// Router,
|
2021-11-01 22:13:37 +01:00
|
|
|
/// routing::{MethodFilter, service_method_routing as service},
|
2021-08-18 00:04:15 +02:00
|
|
|
/// };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// 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()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // Requests to `POST /` will go to `service`.
|
2021-08-19 22:37:48 +02:00
|
|
|
/// let app = Router::new().route("/", service::on(MethodFilter::POST, service));
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// ```
|
2021-10-25 23:28:22 +02:00
|
|
|
pub fn on<S, B>(method: MethodFilter, svc: S) -> MethodRouter<S, MethodNotAllowed<S::Error>, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
S: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-10-24 22:05:16 +02:00
|
|
|
MethodRouter {
|
2021-06-08 12:43:16 +02:00
|
|
|
method,
|
2021-08-15 23:01:26 +02:00
|
|
|
svc,
|
2021-10-25 23:28:22 +02:00
|
|
|
fallback: MethodNotAllowed::new(),
|
2021-08-15 23:01:26 +02:00
|
|
|
_request_body: PhantomData,
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A [`Service`] that accepts requests based on a [`MethodFilter`] and allows
|
|
|
|
|
/// chaining additional services.
|
2021-08-15 23:01:26 +02:00
|
|
|
#[derive(Debug)] // TODO(david): don't require debug for B
|
2021-10-24 22:05:16 +02:00
|
|
|
pub struct MethodRouter<S, F, B> {
|
2021-06-08 12:43:16 +02:00
|
|
|
pub(crate) method: MethodFilter,
|
|
|
|
|
pub(crate) svc: S,
|
|
|
|
|
pub(crate) fallback: F,
|
2021-08-15 23:01:26 +02:00
|
|
|
pub(crate) _request_body: PhantomData<fn() -> B>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
impl<S, F, B> Clone for MethodRouter<S, F, B>
|
2021-08-15 23:01:26 +02:00
|
|
|
where
|
|
|
|
|
S: Clone,
|
|
|
|
|
F: Clone,
|
|
|
|
|
{
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
method: self.method,
|
|
|
|
|
svc: self.svc.clone(),
|
|
|
|
|
fallback: self.fallback.clone(),
|
|
|
|
|
_request_body: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
impl<S, F, B> MethodRouter<S, F, B> {
|
2021-06-08 12:43:16 +02:00
|
|
|
/// Chain an additional service that will accept all requests regardless of
|
|
|
|
|
/// its HTTP method.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn any<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::all(), svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `DELETE` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn delete<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::DELETE, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `GET` requests.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2021-08-18 00:04:15 +02:00
|
|
|
/// use axum::{
|
|
|
|
|
/// http::Request,
|
2021-08-19 22:37:48 +02:00
|
|
|
/// Router,
|
2021-11-01 22:13:37 +01:00
|
|
|
/// routing::{MethodFilter, on, service_method_routing as service},
|
2021-08-18 00:04:15 +02:00
|
|
|
/// };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// 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()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// let other_service = tower::service_fn(|request: Request<Body>| async {
|
|
|
|
|
/// Ok::<_, Infallible>(Response::new(Body::empty()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // Requests to `GET /` will go to `service` and `POST /` will go to
|
|
|
|
|
/// // `other_service`.
|
2021-08-19 22:37:48 +02:00
|
|
|
/// let app = Router::new().route("/", service::post(service).get(other_service));
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// ```
|
2021-08-15 20:27:13 +02:00
|
|
|
///
|
|
|
|
|
/// 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.
|
2021-10-24 22:05:16 +02:00
|
|
|
pub fn get<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-15 20:27:13 +02:00
|
|
|
self.on(MethodFilter::GET | MethodFilter::HEAD, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `HEAD` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn head<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::HEAD, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `OPTIONS` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn options<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::OPTIONS, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `PATCH` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn patch<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::PATCH, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `POST` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn post<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::POST, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `PUT` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn put<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::PUT, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will only accept `TRACE` requests.
|
|
|
|
|
///
|
2021-10-24 22:05:16 +02:00
|
|
|
/// See [`MethodRouter::get`] for an example.
|
|
|
|
|
pub fn trace<T>(self, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-08-07 23:05:53 +02:00
|
|
|
self.on(MethodFilter::TRACE, svc)
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chain an additional service that will accept requests matching the given
|
|
|
|
|
/// `MethodFilter`.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2021-08-18 00:04:15 +02:00
|
|
|
/// use axum::{
|
|
|
|
|
/// http::Request,
|
2021-08-19 22:37:48 +02:00
|
|
|
/// Router,
|
2021-11-01 22:13:37 +01:00
|
|
|
/// routing::{MethodFilter, on, service_method_routing as service},
|
2021-08-18 00:04:15 +02:00
|
|
|
/// };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// 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()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// let other_service = tower::service_fn(|request: Request<Body>| async {
|
|
|
|
|
/// Ok::<_, Infallible>(Response::new(Body::empty()))
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // Requests to `DELETE /` will go to `service`
|
2021-08-19 22:37:48 +02:00
|
|
|
/// let app = Router::new().route("/", service::on(MethodFilter::DELETE, service));
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # async {
|
2021-08-04 15:38:51 +02:00
|
|
|
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
|
2021-06-19 12:50:33 +02:00
|
|
|
/// # };
|
2021-06-08 12:43:16 +02:00
|
|
|
/// ```
|
2021-10-24 22:05:16 +02:00
|
|
|
pub fn on<T>(self, method: MethodFilter, svc: T) -> MethodRouter<T, Self, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-07-05 16:18:39 +02:00
|
|
|
T: Service<Request<B>> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-10-24 22:05:16 +02:00
|
|
|
MethodRouter {
|
2021-06-08 12:43:16 +02:00
|
|
|
method,
|
2021-08-15 23:01:26 +02:00
|
|
|
svc,
|
2021-06-08 12:43:16 +02:00
|
|
|
fallback: self,
|
2021-08-15 23:01:26 +02:00
|
|
|
_request_body: PhantomData,
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
impl<S, F, B, ResBody> Service<Request<B>> for MethodRouter<S, F, B>
|
2021-06-08 12:43:16 +02:00
|
|
|
where
|
2021-08-15 23:01:26 +02:00
|
|
|
S: Service<Request<B>, Response = Response<ResBody>> + Clone,
|
|
|
|
|
ResBody: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
|
|
|
|
ResBody::Error: Into<BoxError>,
|
2021-07-05 16:18:39 +02:00
|
|
|
F: Service<Request<B>, Response = Response<BoxBody>, Error = S::Error> + Clone,
|
2021-06-08 12:43:16 +02:00
|
|
|
{
|
2021-06-13 10:10:37 +02:00
|
|
|
type Response = Response<BoxBody>;
|
2021-07-05 16:18:39 +02:00
|
|
|
type Error = S::Error;
|
2021-10-24 22:05:16 +02:00
|
|
|
type Future = MethodRouterFuture<S, F, B>;
|
2021-06-08 12:43:16 +02:00
|
|
|
|
2021-10-26 01:23:52 +02:00
|
|
|
#[inline]
|
2021-06-08 12:43:16 +02:00
|
|
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 12:50:33 +02:00
|
|
|
fn call(&mut self, req: Request<B>) -> Self::Future {
|
2021-08-15 20:27:13 +02:00
|
|
|
let req_method = req.method().clone();
|
|
|
|
|
|
2021-08-07 22:27:27 +02:00
|
|
|
let f = if self.method.matches(req.method()) {
|
2021-06-12 23:59:18 +02:00
|
|
|
let fut = self.svc.clone().oneshot(req);
|
2021-08-15 23:01:26 +02:00
|
|
|
Either::A { inner: fut }
|
2021-06-08 12:43:16 +02:00
|
|
|
} else {
|
2021-06-12 23:59:18 +02:00
|
|
|
let fut = self.fallback.clone().oneshot(req);
|
2021-08-15 23:01:26 +02:00
|
|
|
Either::B { inner: fut }
|
2021-08-07 22:27:27 +02:00
|
|
|
};
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
MethodRouterFuture {
|
2021-08-15 20:27:13 +02:00
|
|
|
inner: f,
|
|
|
|
|
req_method,
|
|
|
|
|
}
|
2021-06-08 12:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
pin_project! {
|
|
|
|
|
/// The response future for [`MethodRouter`].
|
|
|
|
|
pub struct MethodRouterFuture<S, F, B>
|
|
|
|
|
where
|
|
|
|
|
S: Service<Request<B>>,
|
|
|
|
|
F: Service<Request<B>>
|
|
|
|
|
{
|
|
|
|
|
#[pin]
|
|
|
|
|
pub(super) inner: Either<
|
|
|
|
|
Oneshot<S, Request<B>>,
|
|
|
|
|
Oneshot<F, Request<B>>,
|
|
|
|
|
>,
|
|
|
|
|
pub(super) req_method: Method,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S, F, B, ResBody> Future for MethodRouterFuture<S, F, B>
|
|
|
|
|
where
|
|
|
|
|
S: Service<Request<B>, Response = Response<ResBody>> + Clone,
|
|
|
|
|
ResBody: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
|
|
|
|
ResBody::Error: Into<BoxError>,
|
|
|
|
|
F: Service<Request<B>, Response = Response<BoxBody>, Error = S::Error>,
|
|
|
|
|
{
|
|
|
|
|
type Output = Result<Response<BoxBody>, S::Error>;
|
|
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
|
|
let response = match this.inner.project() {
|
|
|
|
|
EitherProj::A { inner } => ready!(inner.poll(cx))?.map(box_body),
|
|
|
|
|
EitherProj::B { inner } => ready!(inner.poll(cx))?,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if this.req_method == &Method::HEAD {
|
|
|
|
|
let response = response.map(|_| box_body(Empty::new()));
|
|
|
|
|
Poll::Ready(Ok(response))
|
|
|
|
|
} else {
|
|
|
|
|
Poll::Ready(Ok(response))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 20:59:55 +02:00
|
|
|
#[test]
|
|
|
|
|
fn traits() {
|
|
|
|
|
use crate::tests::*;
|
|
|
|
|
|
2021-10-24 22:05:16 +02:00
|
|
|
assert_send::<MethodRouter<(), (), NotSendSync>>();
|
|
|
|
|
assert_sync::<MethodRouter<(), (), NotSendSync>>();
|
2021-08-26 20:59:55 +02:00
|
|
|
}
|