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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! `boluo`是一个简单易用的异步网络框架。
//!
//! # 目录
//!
//! - [快速开始](#快速开始)
//! - [服务](#服务)
//! - [处理程序](#处理程序)
//! - [提取器](#提取器)
//! - [响应](#响应)
//! - [路由](#路由)
//! - [错误处理](#错误处理)
//! - [中间件](#中间件)
//!
//! # 快速开始
//!
//! 新建项目:
//!
//! ```bash
//! cargo new demo && cd demo
//! ```
//!
//! 添加依赖:
//!
//! ```toml
//! [dependencies]
//! boluo = "0.5"
//! tokio = { version = "1", features = ["full"] }
//! ```
//!
//! 用以下内容覆盖`src/main.rs`:
//!
//! ```no_run
//! use boluo::response::IntoResponse;
//! use boluo::route::Router;
//! use boluo::server::Server;
//! use tokio::net::TcpListener;
//!
//! #[tokio::main]
//! async fn main() {
//!     let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
//!
//!     let app = Router::new().mount(hello);
//!
//!     Server::new(listener).run(app).await.unwrap();
//! }
//!
//! #[boluo::route("/", method = "GET")]
//! async fn hello() -> impl IntoResponse {
//!     "Hello, World!"
//! }
//! ```
//!
//! 运行项目:
//!
//! ```bash
//! cargo run
//! ```
//!
//! 访问服务:
//!
//! ```bash
//! curl http://127.0.0.1:3000/
//! ```
//!
//! # 服务
//!
//! [`Service`]特征表示一个接收请求并返回响应的异步函数。
//!
//! # 处理程序
//!
//! 处理程序是一个异步函数,它接受零个或多个提取器作为参数,并返回可以转换为响应的内容。
//!
//! 处理程序如下所示:
//!
//! ```
//! use boluo::body::Body;
//! use boluo::handler::handler_fn;
//! use boluo::response::IntoResponse;
//!
//! // 返回空的`200 OK`响应的处理程序。
//! async fn empty() {}
//!
//! // 返回带有纯文本主体的`200 OK`响应的处理程序。
//! async fn hello() -> &'static str {
//!     "Hello, World!"
//! }
//!
//! // 返回带有请求主体的`200 OK`响应的处理程序。
//! //
//! // `Body`实现了`FromRequest`特征,可以作为提取器解析请求。并且也实现了
//! // `IntoResponse`特征,可以作为响应类型。
//! async fn echo(body: Body) -> impl IntoResponse {
//!     body
//! }
//!
//! // 使用`handler_fn`函数将处理程序转换为`Service`。
//! let service = handler_fn(echo);
//! ```
//!
//! # 提取器
//!
//! 提取器是实现了[`FromRequest`]特征的类型,可以根据[`Request`]创建实例。
//!
//! ```
//! use std::convert::Infallible;
//!
//! use boluo::extract::FromRequest;
//! use boluo::http::{header, HeaderValue};
//! use boluo::request::Request;
//!
//! // 从请求头中提取HOST的提取器。
//! struct Host(Option<HeaderValue>);
//!
//! // 为提取器实现`FromRequest`特征。
//! impl FromRequest for Host {
//!     type Error = Infallible;
//!
//!     async fn from_request(req: &mut Request) -> Result<Self, Self::Error> {
//!         let value = req.headers().get(header::HOST).map(|v| v.to_owned());
//!         Ok(Host(value))
//!     }
//! }
//!
//! // 在处理程序中使用提取器从请求中提取数据。
//! async fn using_extractor(Host(host): Host) {
//!     println!("{host:?}")
//! }
//! ```
//!
//! # 响应
//!
//! 任何实现[`IntoResponse`]特征的类型都可以作为响应。
//!
//! ```
//! use boluo::response::Html;
//! use boluo::response::IntoResponse;
//!
//! // 返回带有纯文本主体的`200 OK`响应的处理程序。
//! async fn hello() -> &'static str {
//!     "Hello, World!"
//! }
//!
//! // 返回显示`Hello, World!`的HTML页面的处理程序。
//! async fn html() -> impl IntoResponse {
//!     Html("<html><body>Hello, World!</body></html>")
//! }
//! ```
//!
//! # 路由
//!
//! [`Router`]用于设置哪些路径通向哪些服务。
//!
//! ```
//! use boluo::handler::handler_fn;
//! use boluo::route::Router;
//!
//! #[boluo::route("/f", method = "GET")]
//! async fn f() -> &'static str {
//!     "f"
//! }
//!
//! let ab = Router::new()
//!     .route("/a", handler_fn(|| async { "a" }))
//!     .route("/b", handler_fn(|| async { "b" }));
//!
//! let cd = Router::new()
//!     .route("/c", handler_fn(|| async { "c" }))
//!     .route("/d", handler_fn(|| async { "d" }));
//!
//! Router::new()
//!     // 路由。
//!     .route("/a", handler_fn(|| async { "a" }))
//!     .route("/b", handler_fn(|| async { "b" }))
//!     // 嵌套路由。
//!     .scope("/x", ab)
//!     // 将其他路由器的路由合并到当前路由器。
//!     .merge(cd)
//!     // 挂载宏定义路由。
//!     .mount(f);
//! ```
//!
//! # 错误处理
//!
//! 错误和响应是分离的,可以在中间件对特定错误进行捕获,并将错误转换为自己想要的响应格式。
//!
//! 未经处理的错误到达服务器时,服务器将返回带有错误信息的`500 INTERNAL_SERVER_ERROR`响应。
//!
//! ```
//! use boluo::http::StatusCode;
//! use boluo::response::{IntoResponse, Response};
//! use boluo::route::{RouteError, RouteErrorKind, Router};
//! use boluo::service::ServiceExt;
//! use boluo::BoxError;
//!
//! #[derive(Debug)]
//! struct MyError;
//!
//! impl std::fmt::Display for MyError {
//!     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//!         write!(f, "some error message")
//!     }
//! }
//!
//! impl std::error::Error for MyError {}
//!
//! // 处理程序。
//! #[boluo::route("/", method = "GET")]
//! async fn throw_error() -> Result<(), MyError> {
//!     Err(MyError)
//! }
//!
//! // 错误处理。
//! async fn handle_error(err: BoxError) -> Result<Response, BoxError> {
//!     // 处理框架抛出的路由错误,并自定义响应方式。
//!     if let Some(e) = err.downcast_ref::<RouteError>() {
//!         let status_code = match e.kind() {
//!             RouteErrorKind::NotFound => StatusCode::NOT_FOUND,
//!             RouteErrorKind::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
//!         };
//!         return Ok((status_code, format!("{status_code}")).into_response()?);
//!     }
//!     if let Some(_) = err.downcast_ref::<MyError>() {
//!         // 记录错误、转为响应等等。
//!     }
//!     Err(err)
//! }
//!
//! Router::new().mount(throw_error).or_else(handle_error);
//! ```
//!
//! # 中间件
//!
//! 中间件是实现了[`Middleware`]特征的类型,可以调用[`ServiceExt::with`]函数将中间件
//! 应用于[`Service`]。
//!
//! 中间件实际上是将原始服务转换为新的服务。
//!
//! ```
//! use boluo::data::Extension;
//! use boluo::handler::handler_fn;
//! use boluo::service::ServiceExt;
//!
//! async fn extension(Extension(text): Extension<&'static str>) -> &'static str {
//!     text
//! }
//!
//! let service = handler_fn(extension);
//! let service = service.with(Extension("Hello, World!"));
//! ```
//!
//! [`Service`]: crate::service::Service
//! [`FromRequest`]: crate::extract::FromRequest
//! [`Request`]: crate::request::Request
//! [`IntoResponse`]: crate::response::IntoResponse
//! [`Router`]: crate::route::Router
//! [`Middleware`]: crate::middleware::Middleware
//! [`ServiceExt::with`]: crate::service::ServiceExt::with

#![forbid(unsafe_code)]
#![warn(
    missing_debug_implementations,
    missing_docs,
    rust_2018_idioms,
    unreachable_pub
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]

pub use boluo_core::BoxError;
pub use boluo_core::{body, handler, http, request, service};

pub use boluo_macros::route;

pub mod data;
pub mod extract;
pub mod middleware;
pub mod response;
pub mod route;

pub use headers;

#[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))]
pub mod server;

#[cfg(feature = "listener")]
pub mod listener;

#[cfg(feature = "multipart")]
pub mod multipart;

#[cfg(feature = "ws")]
pub mod ws;

#[cfg(feature = "fs")]
pub mod fs;