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
//! A minimal, Express-style web framework: simple routing, simple middleware.
//!
//! ```no_run
//! use rustyweb::prelude::*;
//!
//! fn main() {
//! let mut app = App::new();
//! app.middleware(logger);
//!
//! app.get("/", |_req| async { "Hello World!" });
//! app.get("/users/:id", |req| async move {
//! format!("user {}", req.param("id"))
//! });
//! app.get("/teapot", |_req| async { (StatusCode::IM_A_TEAPOT, "short and stout") });
//!
//! app.listen(3000); // http://localhost:3000
//! }
//! ```
//!
//! - **Handlers** are `async` functions or closures taking a [`Request`] and
//! returning anything that implements [`IntoResponse`].
//! - **Middleware** is an `async` function taking a [`Request`] and [`Next`].
//! Call `next.run(req).await` to continue, or return a response directly to
//! short-circuit. Code after `next.run` runs once the handler has responded.
//! - **Routes** match in registration order. `:name` captures a segment,
//! a trailing `*` captures the rest of the path.
pub use ;
pub use Body;
pub use ;
pub use Error;
pub use ;
pub use Json;
pub use logger;
pub use ;
pub use Request;
pub use ;
pub use ;
/// Any JSON value. Read a body without declaring a struct:
/// `let body: Value = req.json()?;` then `body["name"]`.
pub use Value;
/// Build JSON inline, like a JavaScript object literal:
/// `Json(json!({ "ok": true, "items": [1, 2, 3] }))`.
pub use json;
pub use ListenAddr;
pub use ;
/// `Result` with [`Error`] as the default error type, so handlers that can
/// fail read simply:
///
/// ```
/// use rustyweb::prelude::*;
///
/// async fn hello(req: Request) -> Result<String> {
/// let name = req.text()?; // not UTF-8? the client gets a 400
/// Ok(format!("hello {name}"))
/// }
/// ```
///
/// Other error types still work as usual: `Result<T, std::io::Error>`.
pub type Result<T, E = Error> = Result;
/// Connection upgrades, the building block for WebSocket and other protocol
/// packages.
///
/// ```no_run
/// use rustyweb::{App, IntoResponse, Request, StatusCode, header};
/// use rustyweb::upgrade::TokioIo;
/// use tokio::io::{AsyncReadExt, AsyncWriteExt};
///
/// let mut app = App::new();
/// // An "echo" protocol: whatever the client sends comes back.
/// app.get("/echo", |mut req: Request| async move {
/// let Some(on_upgrade) = req.upgrade() else {
/// return StatusCode::BAD_REQUEST.into_response();
/// };
/// tokio::spawn(async move {
/// let Ok(upgraded) = on_upgrade.await else { return };
/// let mut io = TokioIo::new(upgraded);
/// let mut buf = [0; 1024];
/// while let Ok(n @ 1..) = io.read(&mut buf).await {
/// if io.write_all(&buf[..n]).await.is_err() {
/// break;
/// }
/// }
/// });
/// let mut res = StatusCode::SWITCHING_PROTOCOLS.into_response();
/// res.headers_mut().insert(header::UPGRADE, "echo".parse().unwrap());
/// res.headers_mut().insert(header::CONNECTION, "upgrade".parse().unwrap());
/// res
/// });
/// ```
/// Everything most apps need, in one import: `use rustyweb::prelude::*;`