1 use super::Handler;
2 #[cfg(feature = "tokio")]
3 use crate::extract::connect_info::IntoMakeServiceWithConnectInfo;
4 use crate::response::Response;
5 use crate::routing::IntoMakeService;
6 use http::Request;
7 use std::{
8     convert::Infallible,
9     fmt,
10     marker::PhantomData,
11     task::{Context, Poll},
12 };
13 use tower_service::Service;
14 
15 /// An adapter that makes a [`Handler`] into a [`Service`].
16 ///
17 /// Created with [`Handler::with_state`] or [`HandlerWithoutStateExt::into_service`].
18 ///
19 /// [`HandlerWithoutStateExt::into_service`]: super::HandlerWithoutStateExt::into_service
20 pub struct HandlerService<H, T, S, B> {
21     handler: H,
22     state: S,
23     _marker: PhantomData<fn() -> (T, B)>,
24 }
25 
26 impl<H, T, S, B> HandlerService<H, T, S, B> {
27     /// Get a reference to the state.
state(&self) -> &S28     pub fn state(&self) -> &S {
29         &self.state
30     }
31 
32     /// Convert the handler into a [`MakeService`].
33     ///
34     /// This allows you to serve a single handler if you don't need any routing:
35     ///
36     /// ```rust
37     /// use axum::{
38     ///     Server,
39     ///     handler::Handler,
40     ///     extract::State,
41     ///     http::{Uri, Method},
42     ///     response::IntoResponse,
43     /// };
44     /// use std::net::SocketAddr;
45     ///
46     /// #[derive(Clone)]
47     /// struct AppState {}
48     ///
49     /// async fn handler(State(state): State<AppState>) {
50     ///     // ...
51     /// }
52     ///
53     /// let app = handler.with_state(AppState {});
54     ///
55     /// # async {
56     /// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
57     ///     .serve(app.into_make_service())
58     ///     .await?;
59     /// # Ok::<_, hyper::Error>(())
60     /// # };
61     /// ```
62     ///
63     /// [`MakeService`]: tower::make::MakeService
into_make_service(self) -> IntoMakeService<HandlerService<H, T, S, B>>64     pub fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, S, B>> {
65         IntoMakeService::new(self)
66     }
67 
68     /// Convert the handler into a [`MakeService`] which stores information
69     /// about the incoming connection.
70     ///
71     /// See [`Router::into_make_service_with_connect_info`] for more details.
72     ///
73     /// ```rust
74     /// use axum::{
75     ///     Server,
76     ///     handler::Handler,
77     ///     response::IntoResponse,
78     ///     extract::{ConnectInfo, State},
79     /// };
80     /// use std::net::SocketAddr;
81     ///
82     /// #[derive(Clone)]
83     /// struct AppState {};
84     ///
85     /// async fn handler(
86     ///     ConnectInfo(addr): ConnectInfo<SocketAddr>,
87     ///     State(state): State<AppState>,
88     /// ) -> String {
89     ///     format!("Hello {}", addr)
90     /// }
91     ///
92     /// let app = handler.with_state(AppState {});
93     ///
94     /// # async {
95     /// Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
96     ///     .serve(app.into_make_service_with_connect_info::<SocketAddr>())
97     ///     .await?;
98     /// # Ok::<_, hyper::Error>(())
99     /// # };
100     /// ```
101     ///
102     /// [`MakeService`]: tower::make::MakeService
103     /// [`Router::into_make_service_with_connect_info`]: crate::routing::Router::into_make_service_with_connect_info
104     #[cfg(feature = "tokio")]
into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, S, B>, C>105     pub fn into_make_service_with_connect_info<C>(
106         self,
107     ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, S, B>, C> {
108         IntoMakeServiceWithConnectInfo::new(self)
109     }
110 }
111 
112 #[test]
traits()113 fn traits() {
114     use crate::test_helpers::*;
115     assert_send::<HandlerService<(), NotSendSync, (), NotSendSync>>();
116     assert_sync::<HandlerService<(), NotSendSync, (), NotSendSync>>();
117 }
118 
119 impl<H, T, S, B> HandlerService<H, T, S, B> {
new(handler: H, state: S) -> Self120     pub(super) fn new(handler: H, state: S) -> Self {
121         Self {
122             handler,
123             state,
124             _marker: PhantomData,
125         }
126     }
127 }
128 
129 impl<H, T, S, B> fmt::Debug for HandlerService<H, T, S, B> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result130     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131         f.debug_struct("IntoService").finish_non_exhaustive()
132     }
133 }
134 
135 impl<H, T, S, B> Clone for HandlerService<H, T, S, B>
136 where
137     H: Clone,
138     S: Clone,
139 {
clone(&self) -> Self140     fn clone(&self) -> Self {
141         Self {
142             handler: self.handler.clone(),
143             state: self.state.clone(),
144             _marker: PhantomData,
145         }
146     }
147 }
148 
149 impl<H, T, S, B> Service<Request<B>> for HandlerService<H, T, S, B>
150 where
151     H: Handler<T, S, B> + Clone + Send + 'static,
152     B: Send + 'static,
153     S: Clone + Send + Sync,
154 {
155     type Response = Response;
156     type Error = Infallible;
157     type Future = super::future::IntoServiceFuture<H::Future>;
158 
159     #[inline]
poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>160     fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
161         // `IntoService` can only be constructed from async functions which are always ready, or
162         // from `Layered` which buffers in `<Layered as Handler>::call` and is therefore
163         // also always ready.
164         Poll::Ready(Ok(()))
165     }
166 
call(&mut self, req: Request<B>) -> Self::Future167     fn call(&mut self, req: Request<B>) -> Self::Future {
168         use futures_util::future::FutureExt;
169 
170         let handler = self.handler.clone();
171         let future = Handler::call(handler, req, self.state.clone());
172         let future = future.map(Ok as _);
173 
174         super::future::IntoServiceFuture::new(future)
175     }
176 }
177