axum/handler/mod.rs
1//! Async functions that can be used to handle requests.
2//!
3#![doc = include_str!("../docs/handlers_intro.md")]
4//!
5//! Some examples of handlers:
6//!
7//! ```rust
8//! use axum::{body::Bytes, http::StatusCode};
9//!
10//! // Handler that immediately returns an empty `200 OK` response.
11//! async fn unit_handler() {}
12//!
13//! // Handler that immediately returns a `200 OK` response with a plain text
14//! // body.
15//! async fn string_handler() -> String {
16//! "Hello, World!".to_string()
17//! }
18//!
19//! // Handler that buffers the request body and returns it.
20//! //
21//! // This works because `Bytes` implements `FromRequest`
22//! // and therefore can be used as an extractor.
23//! //
24//! // `String` and `StatusCode` both implement `IntoResponse` and
25//! // therefore `Result<String, StatusCode>` also implements `IntoResponse`
26//! async fn echo(body: Bytes) -> Result<String, StatusCode> {
27//! if let Ok(string) = String::from_utf8(body.to_vec()) {
28//! Ok(string)
29//! } else {
30//! Err(StatusCode::BAD_REQUEST)
31//! }
32//! }
33//! ```
34//!
35//! Instead of a direct `StatusCode`, it makes sense to use intermediate error type
36//! that can ultimately be converted to `Response`. This allows using `?` operator
37//! in handlers. See those examples:
38//!
39//! * [`anyhow-error-response`][anyhow] for generic boxed errors
40//! * [`error-handling`][error-handling] for application-specific detailed errors
41//!
42//! [anyhow]: https://github.com/tokio-rs/axum/blob/main/examples/anyhow-error-response/src/main.rs
43//! [error-handling]: https://github.com/tokio-rs/axum/blob/main/examples/error-handling/src/main.rs
44//!
45#![doc = include_str!("../docs/debugging_handler_type_errors.md")]
46
47#[cfg(feature = "tokio")]
48use crate::extract::connect_info::IntoMakeServiceWithConnectInfo;
49use crate::{
50 extract::{FromRequest, FromRequestParts, Request},
51 response::{IntoResponse, Response},
52 routing::IntoMakeService,
53};
54use std::{convert::Infallible, fmt, future::Future, marker::PhantomData, pin::Pin};
55use tower::ServiceExt;
56use tower_layer::Layer;
57use tower_service::Service;
58
59pub mod future;
60mod service;
61
62pub use self::service::HandlerService;
63
64/// Trait for async functions that can be used to handle requests.
65///
66/// You shouldn't need to depend on this trait directly. It is automatically
67/// implemented to closures of the right types.
68///
69/// See the [module docs](crate::handler) for more details.
70///
71/// # Converting `Handler`s into [`Service`]s
72///
73/// To convert `Handler`s into [`Service`]s you have to call either
74/// [`HandlerWithoutStateExt::into_service`] or [`Handler::with_state`]:
75///
76/// ```
77/// use tower::Service;
78/// use axum::{
79/// extract::{State, Request},
80/// body::Body,
81/// handler::{HandlerWithoutStateExt, Handler},
82/// };
83///
84/// // this handler doesn't require any state
85/// async fn one() {}
86/// // so it can be converted to a service with `HandlerWithoutStateExt::into_service`
87/// assert_service(one.into_service());
88///
89/// // this handler requires state
90/// async fn two(_: State<String>) {}
91/// // so we have to provide it
92/// let handler_with_state = two.with_state(String::new());
93/// // which gives us a `Service`
94/// assert_service(handler_with_state);
95///
96/// // helper to check that a value implements `Service`
97/// fn assert_service<S>(service: S)
98/// where
99/// S: Service<Request>,
100/// {}
101/// ```
102#[doc = include_str!("../docs/debugging_handler_type_errors.md")]
103///
104/// # Handlers that aren't functions
105///
106/// The `Handler` trait is also implemented for `T: IntoResponse`. That allows easily returning
107/// fixed data for routes:
108///
109/// ```
110/// use axum::{
111/// Router,
112/// routing::{get, post},
113/// Json,
114/// http::StatusCode,
115/// };
116/// use serde_json::json;
117///
118/// let app = Router::new()
119/// // respond with a fixed string
120/// .route("/", get("Hello, World!"))
121/// // or return some mock data
122/// .route("/users", post((
123/// StatusCode::CREATED,
124/// Json(json!({ "id": 1, "username": "alice" })),
125/// )));
126/// # let _: Router = app;
127/// ```
128///
129/// # About type parameter `T`
130///
131/// **Generally you shouldn't need to worry about `T`**; when calling methods such as
132/// [`post`](crate::routing::method_routing::post) it will be automatically inferred and this is
133/// the intended way for this parameter to be provided in application code.
134///
135/// If you are implementing your own methods that accept implementations of `Handler` as
136/// arguments, then the following may be useful:
137///
138/// The type parameter `T` is a workaround for trait coherence rules, allowing us to
139/// write blanket implementations of `Handler` over many types of handler functions
140/// with different numbers of arguments, without the compiler forbidding us from doing
141/// so because one type `F` can in theory implement both `Fn(A) -> X` and `Fn(A, B) -> Y`.
142/// `T` is a placeholder taking on a representation of the parameters of the handler function,
143/// as well as other similar 'coherence rule workaround' discriminators,
144/// allowing us to select one function signature to use as a `Handler`.
145#[diagnostic::on_unimplemented(
146 note = "Consider using `#[axum::debug_handler]` to improve the error message"
147)]
148pub trait Handler<T, S>: Clone + Send + Sync + Sized + 'static {
149 /// The type of future calling this handler returns.
150 type Future: Future<Output = Response> + Send + 'static;
151
152 /// Call the handler with the given request.
153 fn call(self, req: Request, state: S) -> Self::Future;
154
155 /// Apply a [`tower::Layer`] to the handler.
156 ///
157 /// All requests to the handler will be processed by the layer's
158 /// corresponding middleware.
159 ///
160 /// This can be used to add additional processing to a request for a single
161 /// handler.
162 ///
163 /// Note this differs from [`routing::Router::layer`](crate::routing::Router::layer)
164 /// which adds a middleware to a group of routes.
165 ///
166 /// If you're applying middleware that produces errors you have to handle the errors
167 /// so they're converted into responses. You can learn more about doing that
168 /// [here](crate::error_handling).
169 ///
170 /// # Example
171 ///
172 /// Adding the [`tower::limit::ConcurrencyLimit`] middleware to a handler
173 /// can be done like so:
174 ///
175 /// ```rust
176 /// use axum::{
177 /// routing::get,
178 /// handler::Handler,
179 /// Router,
180 /// };
181 /// use tower::limit::{ConcurrencyLimitLayer, ConcurrencyLimit};
182 ///
183 /// async fn handler() { /* ... */ }
184 ///
185 /// let layered_handler = handler.layer(ConcurrencyLimitLayer::new(64));
186 /// let app = Router::new().route("/", get(layered_handler));
187 /// # let _: Router = app;
188 /// ```
189 fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>
190 where
191 L: Layer<HandlerService<Self, T, S>> + Clone,
192 L::Service: Service<Request>,
193 {
194 Layered {
195 layer,
196 handler: self,
197 _marker: PhantomData,
198 }
199 }
200
201 /// Convert the handler into a [`Service`] by providing the state
202 fn with_state(self, state: S) -> HandlerService<Self, T, S> {
203 HandlerService::new(self, state)
204 }
205}
206
207impl<F, Fut, Res, S> Handler<((),), S> for F
208where
209 F: FnOnce() -> Fut + Clone + Send + Sync + 'static,
210 Fut: Future<Output = Res> + Send,
211 Res: IntoResponse,
212{
213 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
214
215 fn call(self, _req: Request, _state: S) -> Self::Future {
216 Box::pin(async move { self().await.into_response() })
217 }
218}
219
220macro_rules! impl_handler {
221 (
222 [$($ty:ident),*], $last:ident
223 ) => {
224 #[allow(non_snake_case, unused_mut)]
225 impl<F, Fut, S, Res, M, $($ty,)* $last> Handler<(M, $($ty,)* $last,), S> for F
226 where
227 F: FnOnce($($ty,)* $last,) -> Fut + Clone + Send + Sync + 'static,
228 Fut: Future<Output = Res> + Send,
229 S: Send + Sync + 'static,
230 Res: IntoResponse,
231 $( $ty: FromRequestParts<S> + Send, )*
232 $last: FromRequest<S, M> + Send,
233 {
234 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
235
236 fn call(self, req: Request, state: S) -> Self::Future {
237 let (mut parts, body) = req.into_parts();
238 Box::pin(async move {
239 $(
240 let $ty = match $ty::from_request_parts(&mut parts, &state).await {
241 Ok(value) => value,
242 Err(rejection) => return rejection.into_response(),
243 };
244 )*
245
246 let req = Request::from_parts(parts, body);
247
248 let $last = match $last::from_request(req, &state).await {
249 Ok(value) => value,
250 Err(rejection) => return rejection.into_response(),
251 };
252
253 self($($ty,)* $last,).await.into_response()
254 })
255 }
256 }
257 };
258}
259
260all_the_tuples!(impl_handler);
261
262mod private {
263 // Marker type for `impl<T: IntoResponse> Handler for T`
264 #[allow(missing_debug_implementations)]
265 pub enum IntoResponseHandler {}
266}
267
268impl<T, S> Handler<private::IntoResponseHandler, S> for T
269where
270 T: IntoResponse + Clone + Send + Sync + 'static,
271{
272 type Future = std::future::Ready<Response>;
273
274 fn call(self, _req: Request, _state: S) -> Self::Future {
275 std::future::ready(self.into_response())
276 }
277}
278
279/// A [`Service`] created from a [`Handler`] by applying a Tower middleware.
280///
281/// Created with [`Handler::layer`]. See that method for more details.
282pub struct Layered<L, H, T, S> {
283 layer: L,
284 handler: H,
285 _marker: PhantomData<fn() -> (T, S)>,
286}
287
288impl<L, H, T, S> fmt::Debug for Layered<L, H, T, S>
289where
290 L: fmt::Debug,
291{
292 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293 f.debug_struct("Layered")
294 .field("layer", &self.layer)
295 .finish()
296 }
297}
298
299impl<L, H, T, S> Clone for Layered<L, H, T, S>
300where
301 L: Clone,
302 H: Clone,
303{
304 fn clone(&self) -> Self {
305 Self {
306 layer: self.layer.clone(),
307 handler: self.handler.clone(),
308 _marker: PhantomData,
309 }
310 }
311}
312
313impl<H, S, T, L> Handler<T, S> for Layered<L, H, T, S>
314where
315 L: Layer<HandlerService<H, T, S>> + Clone + Send + Sync + 'static,
316 H: Handler<T, S>,
317 L::Service: Service<Request, Error = Infallible> + Clone + Send + 'static,
318 <L::Service as Service<Request>>::Response: IntoResponse,
319 <L::Service as Service<Request>>::Future: Send,
320 T: 'static,
321 S: 'static,
322{
323 type Future = future::LayeredFuture<L::Service>;
324
325 fn call(self, req: Request, state: S) -> Self::Future {
326 use futures_util::future::{FutureExt, Map};
327
328 let svc = self.handler.with_state(state);
329 let svc = self.layer.layer(svc);
330
331 let future: Map<
332 _,
333 fn(
334 Result<
335 <L::Service as Service<Request>>::Response,
336 <L::Service as Service<Request>>::Error,
337 >,
338 ) -> _,
339 > = svc.oneshot(req).map(|result| match result {
340 Ok(res) => res.into_response(),
341 Err(err) => match err {},
342 });
343
344 future::LayeredFuture::new(future)
345 }
346}
347
348/// Extension trait for [`Handler`]s that don't have state.
349///
350/// This provides convenience methods to convert the [`Handler`] into a [`Service`] or [`MakeService`].
351///
352/// [`MakeService`]: tower::make::MakeService
353pub trait HandlerWithoutStateExt<T>: Handler<T, ()> {
354 /// Convert the handler into a [`Service`] and no state.
355 fn into_service(self) -> HandlerService<Self, T, ()>;
356
357 /// Convert the handler into a [`MakeService`] and no state.
358 ///
359 /// See [`HandlerService::into_make_service`] for more details.
360 ///
361 /// [`MakeService`]: tower::make::MakeService
362 fn into_make_service(self) -> IntoMakeService<HandlerService<Self, T, ()>>;
363
364 /// Convert the handler into a [`MakeService`] which stores information
365 /// about the incoming connection and has no state.
366 ///
367 /// See [`HandlerService::into_make_service_with_connect_info`] for more details.
368 ///
369 /// [`MakeService`]: tower::make::MakeService
370 #[cfg(feature = "tokio")]
371 fn into_make_service_with_connect_info<C>(
372 self,
373 ) -> IntoMakeServiceWithConnectInfo<HandlerService<Self, T, ()>, C>;
374}
375
376impl<H, T> HandlerWithoutStateExt<T> for H
377where
378 H: Handler<T, ()>,
379{
380 fn into_service(self) -> HandlerService<Self, T, ()> {
381 self.with_state(())
382 }
383
384 fn into_make_service(self) -> IntoMakeService<HandlerService<Self, T, ()>> {
385 self.into_service().into_make_service()
386 }
387
388 #[cfg(feature = "tokio")]
389 fn into_make_service_with_connect_info<C>(
390 self,
391 ) -> IntoMakeServiceWithConnectInfo<HandlerService<Self, T, ()>, C> {
392 self.into_service().into_make_service_with_connect_info()
393 }
394}
395
396#[cfg(test)]
397mod tests {
398 use super::*;
399 use crate::{extract::State, test_helpers::*};
400 use axum_core::body::Body;
401 use http::StatusCode;
402 use std::time::Duration;
403 use tower_http::{
404 limit::RequestBodyLimitLayer, map_request_body::MapRequestBodyLayer,
405 map_response_body::MapResponseBodyLayer, timeout::TimeoutLayer,
406 };
407
408 #[crate::test]
409 async fn handler_into_service() {
410 async fn handle(body: String) -> impl IntoResponse {
411 format!("you said: {body}")
412 }
413
414 let client = TestClient::new(handle.into_service());
415
416 let res = client.post("/").body("hi there!").await;
417 assert_eq!(res.status(), StatusCode::OK);
418 assert_eq!(res.text().await, "you said: hi there!");
419 }
420
421 #[crate::test]
422 async fn with_layer_that_changes_request_body_and_state() {
423 async fn handle(State(state): State<&'static str>) -> &'static str {
424 state
425 }
426
427 let svc = handle
428 .layer((
429 RequestBodyLimitLayer::new(1024),
430 TimeoutLayer::new(Duration::from_secs(10)),
431 MapResponseBodyLayer::new(Body::new),
432 ))
433 .layer(MapRequestBodyLayer::new(Body::new))
434 .with_state("foo");
435
436 let client = TestClient::new(svc);
437 let res = client.get("/").await;
438 assert_eq!(res.text().await, "foo");
439 }
440}