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
//! HTTP request handler module
//!
//! This module provides traits and utilities for handling HTTP requests. It defines
//! the core abstraction for request processing through the [`Handler`] trait and
//! provides utilities for creating handlers from async functions.
//!
//! # Examples
//!
//! ```no_run
//! use micro_http::handler::{Handler, make_handler};
//! use micro_http::protocol::body::ReqBody;
//! use http::{Request, Response};
//! use std::error::Error;
//!
//! // Define an async handler function
//! async fn hello_handler(req: Request<ReqBody>) -> Result<Response<String>, Box<dyn Error + Send + Sync>> {
//! Ok(Response::new("Hello World!".to_string()))
//! }
//!
//! // Create a handler from the function
//! let handler = make_handler(hello_handler);
//! ```
use crateReqBody;
use ;
use Body;
use Error;
/// A trait for handling HTTP requests
///
/// This trait defines the core interface for processing HTTP requests and generating responses.
/// Implementors of this trait can be used to handle requests in the HTTP server.
///
/// # Type Parameters
///
/// * `RespBody`: The response body type that implements [`Body`]
/// * `Error`: The error type that can be converted into a boxed error
/// * `Fut`: The future type returned by the handler
/// A wrapper type for function-based handlers
///
/// This type implements [`Handler`] for functions that take a [`Request`] and
/// return a [`Future`] resolving to a [`Response`].
/// Creates a new handler from an async function
///
/// This function wraps an async function in a [`HandlerFn`] type that implements
/// the [`Handler`] trait.
///
/// # Arguments
///
/// * `f` - An async function that takes a [`Request`] and returns a [`Future`] resolving to a [`Response`]
///
/// # Examples
///
/// ```no_run
/// use micro_http::handler::make_handler;
/// use http::{Request, Response};
/// use micro_http::protocol::body::ReqBody;
/// use std::error::Error;
///
/// async fn my_handler(req: Request<ReqBody>) -> Result<Response<String>, Box<dyn Error + Send + Sync>> {
/// Ok(Response::new("Hello".to_string()))
/// }
///
/// let handler = make_handler(my_handler);
/// ```