1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
//! Middleware that clones a value into each request's [extensions].
//!
//! [extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
//!
//! # Example
//!
//! ```
//! use tower_http::add_extension::AddExtensionLayer;
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
//! use http::{Request, Response};
//! use bytes::Bytes;
//! use http_body_util::Full;
//! use std::{sync::Arc, convert::Infallible};
//!
//! # struct DatabaseConnectionPool;
//! # impl DatabaseConnectionPool {
//! # fn new() -> DatabaseConnectionPool { DatabaseConnectionPool }
//! # }
//! #
//! // Shared state across all request handlers --- in this case, a pool of database connections.
//! struct State {
//! pool: DatabaseConnectionPool,
//! }
//!
//! async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> {
//! // Grab the state from the request extensions.
//! let state = req.extensions().get::<Arc<State>>().unwrap();
//!
//! Ok(Response::new(Full::default()))
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Construct the shared state.
//! let state = State {
//! pool: DatabaseConnectionPool::new(),
//! };
//!
//! let mut service = ServiceBuilder::new()
//! // Share an `Arc<State>` with all requests.
//! .layer(AddExtensionLayer::new(Arc::new(state)))
//! .service_fn(handle);
//!
//! // Call the service.
//! let response = service
//! .ready()
//! .await?
//! .call(Request::new(Full::default()))
//! .await?;
//! # Ok(())
//! # }
//! ```
use http::{Request, Response};
use std::task::{Context, Poll};
use tower_layer::Layer;
use tower_service::Service;
/// [`Layer`] for adding some shareable value to [request extensions].
///
/// See the [module docs](crate::add_extension) for more details.
///
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
#[derive(Clone, Copy, Debug)]
pub struct AddExtensionLayer<T> {
value: T,
}
impl<T> AddExtensionLayer<T> {
/// Create a new [`AddExtensionLayer`].
pub fn new(value: T) -> Self {
AddExtensionLayer { value }
}
}
impl<S, T> Layer<S> for AddExtensionLayer<T>
where
T: Clone,
{
type Service = AddExtension<S, T>;
fn layer(&self, inner: S) -> Self::Service {
AddExtension {
inner,
value: self.value.clone(),
}
}
}
/// Middleware for adding some shareable value to [request extensions].
///
/// See the [module docs](crate::add_extension) for more details.
///
/// [request extensions]: https://docs.rs/http/latest/http/struct.Extensions.html
#[derive(Clone, Copy, Debug)]
pub struct AddExtension<S, T> {
inner: S,
value: T,
}
impl<S, T> AddExtension<S, T> {
/// Create a new [`AddExtension`].
pub fn new(inner: S, value: T) -> Self {
Self { inner, value }
}
define_inner_service_accessors!();
/// Returns a new [`Layer`] that wraps services with a `AddExtension` middleware.
///
/// [`Layer`]: tower_layer::Layer
pub fn layer(value: T) -> AddExtensionLayer<T> {
AddExtensionLayer::new(value)
}
}
impl<ResBody, ReqBody, S, T> Service<Request<ReqBody>> for AddExtension<S, T>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
T: Clone + Send + Sync + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
req.extensions_mut().insert(self.value.clone());
self.inner.call(req)
}
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use http::Response;
use std::{convert::Infallible, sync::Arc};
use tower::{service_fn, ServiceBuilder, ServiceExt};
struct State(i32);
#[tokio::test]
async fn basic() {
let state = Arc::new(State(1));
let svc = ServiceBuilder::new()
.layer(AddExtensionLayer::new(state))
.service(service_fn(|req: Request<Body>| async move {
let state = req.extensions().get::<Arc<State>>().unwrap();
Ok::<_, Infallible>(Response::new(state.0))
}));
let res = svc
.oneshot(Request::new(Body::empty()))
.await
.unwrap()
.into_body();
assert_eq!(1, res);
}
}