micro_http/handler/mod.rs
1//! HTTP request handler module
2//!
3//! This module provides traits and utilities for handling HTTP requests. It defines
4//! the core abstraction for request processing through the [`Handler`] trait and
5//! provides utilities for creating handlers from async functions.
6//!
7//! # Examples
8//!
9//! ```no_run
10//! use micro_http::handler::{Handler, make_handler};
11//! use micro_http::protocol::body::ReqBody;
12//! use http::{Request, Response};
13//! use std::error::Error;
14//!
15//! // Define an async handler function
16//! async fn hello_handler(req: Request<ReqBody>) -> Result<Response<String>, Box<dyn Error + Send + Sync>> {
17//! Ok(Response::new("Hello World!".to_string()))
18//! }
19//!
20//! // Create a handler from the function
21//! let handler = make_handler(hello_handler);
22//! ```
23
24use crate::protocol::body::ReqBody;
25use http::{Request, Response};
26use http_body::Body;
27use std::error::Error;
28use std::future::Future;
29
30/// A trait for handling HTTP requests
31///
32/// This trait defines the core interface for processing HTTP requests and generating responses.
33/// Implementors of this trait can be used to handle requests in the HTTP server.
34///
35/// # Type Parameters
36///
37/// * `RespBody`: The response body type that implements [`Body`]
38/// * `Error`: The error type that can be converted into a boxed error
39/// * `Fut`: The future type returned by the handler
40#[trait_variant::make(Send)]
41pub trait Handler: Sync {
42 /// The type of the response body
43 type RespBody: Body;
44
45 /// The error type returned by the handler
46 type Error: Into<Box<dyn Error + Send + Sync>>;
47
48 async fn call(&self, req: Request<ReqBody>) -> Result<Response<Self::RespBody>, Self::Error>;
49}
50
51/// A wrapper type for function-based handlers
52///
53/// This type implements [`Handler`] for functions that take a [`Request`] and
54/// return a [`Future`] resolving to a [`Response`].
55#[derive(Debug)]
56pub struct HandlerFn<F> {
57 f: F,
58}
59
60impl<RespBody, Err, F, Fut> Handler for HandlerFn<F>
61where
62 RespBody: Body,
63 F: Fn(Request<ReqBody>) -> Fut + Send + Sync,
64 Err: Into<Box<dyn Error + Send + Sync>>,
65 Fut: Future<Output = Result<Response<RespBody>, Err>> + Send,
66{
67 type RespBody = RespBody;
68 type Error = Err;
69
70 async fn call(&self, req: Request<ReqBody>) -> Result<Response<Self::RespBody>, Self::Error> {
71 (self.f)(req).await
72 }
73}
74
75/// Creates a new handler from an async function
76///
77/// This function wraps an async function in a [`HandlerFn`] type that implements
78/// the [`Handler`] trait.
79///
80/// # Arguments
81///
82/// * `f` - An async function that takes a [`Request`] and returns a [`Future`]
83/// resolving to a [`Response`]
84///
85/// # Examples
86///
87/// ```no_run
88/// use micro_http::handler::make_handler;
89/// use http::{Request, Response};
90/// use micro_http::protocol::body::ReqBody;
91/// use std::error::Error;
92///
93/// async fn my_handler(req: Request<ReqBody>) -> Result<Response<String>, Box<dyn Error + Send + Sync>> {
94/// Ok(Response::new("Hello".to_string()))
95/// }
96///
97/// let handler = make_handler(my_handler);
98/// ```
99pub fn make_handler<F, RespBody, Err, Ret>(f: F) -> HandlerFn<F>
100where
101 RespBody: Body,
102 Err: Into<Box<dyn Error + Send + Sync>>,
103 Ret: Future<Output = Result<Response<RespBody>, Err>>,
104 F: Fn(Request<ReqBody>) -> Ret,
105{
106 HandlerFn { f }
107}