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
//! [![Build status](https://img.shields.io/travis/Hexilee/roa/master.svg)](https://travis-ci.org/Hexilee/roa)
//! [![codecov](https://codecov.io/gh/Hexilee/roa/branch/master/graph/badge.svg)](https://codecov.io/gh/Hexilee/roa)
//! [![Rust Docs](https://docs.rs/roa/badge.svg)](https://docs.rs/roa)
//! [![Crate version](https://img.shields.io/crates/v/roa.svg)](https://crates.io/crates/roa)
//! [![Download](https://img.shields.io/crates/d/roa.svg)](https://crates.io/crates/roa)
//! [![Version](https://img.shields.io/badge/rustc-1.39+-lightgray.svg)](https://blog.rust-lang.org/2019/11/07/Rust-1.39.0.html)
//! [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/Hexilee/roa/blob/master/LICENSE)
//!
//! ### Introduction
//!
//! Roa is an async web framework inspired by koajs, lightweight but powerful.
//!
//! ### Application
//!
//! A Roa application is a structure containing a middleware group
//! which composes and executes middleware functions in a stack-like manner.
//!
//! The obligatory hello world application:
//!
//! ```rust,no_run
//! use roa::core::App;
//! use roa::preload::*;
//! use log::info;
//! use std::error::Error as StdError;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn StdError>> {
//!     let mut app = App::new(());
//!     app.end(|ctx| async move {
//!         ctx.write_text("Hello, World").await
//!     });
//!     app.listen("127.0.0.1:8000", |addr| {
//!         info!("Server is listening on {}", addr)
//!     })?
//!     .await?;
//!     Ok(())
//! }
//! ```
//!
//! #### Cascading
//! Like koajs, middleware suspends and passes control to "downstream" by invoking `next().await`.
//! Then control flows back "upstream" when `next().await` returns.
//!
//! The following example responds with "Hello World",
//! however first the request flows through the x-response-time and logging middleware to mark
//! when the request started, then continue to yield control through the response middleware.
//! When a middleware invokes next() the function suspends and passes control to the next middleware defined.
//! After there are no more middleware to execute downstream,
//! the stack will unwind and each middleware is resumed to perform its upstream behaviour.
//!
//! ```rust,no_run
//! use roa::core::App;
//! use roa::preload::*;
//! use log::info;
//! use std::error::Error as StdError;
//! use std::time::Instant;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn StdError>> {
//!     let mut app = App::new(());
//!     // logger
//!     app.gate_fn(|ctx, next| async move {
//!       next().await?;
//!       let rt = ctx.resp().await.must_get("x-response-time")?.to_owned();
//!       info!("{} {} - {}", ctx.method().await, ctx.uri().await, rt);
//!       Ok(())
//!     });
//!
//!     // x-response-time
//!     app.gate_fn(|ctx, next| async move {
//!         let start = Instant::now();
//!         next().await?;
//!         let ms = start.elapsed().as_millis();
//!         ctx.resp_mut().await.insert("x-response-time", format!("{}ms", ms))?;
//!         Ok(())
//!     });
//!
//!     // response
//!     app.end(|ctx| async move {
//!         ctx.write_text("Hello, World").await
//!     });
//!
//!     app.listen("127.0.0.1:8000", |addr| {
//!         info!("Server is listening on {}", addr)
//!     })?
//!     .await?;
//!     Ok(())
//! }
//! ```
//!
//! ### Error Handling
//!
//! You can catch or straightly throw an error returned by next.
//!
//! ```rust,no_run
//! use roa::core::{App, throw, StatusCode};
//! use async_std::task::spawn;
//! use log::info;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     App::new(())
//!         .gate_fn(|ctx, next| async move {
//!             // catch
//!             if let Err(err) = next().await {
//!                 // teapot is ok
//!                 if err.status_code != StatusCode::IM_A_TEAPOT {
//!                     return Err(err)
//!                 }
//!             }
//!             Ok(())
//!         })
//!         .gate_fn(|ctx, next| async move {
//!             next().await?; // just throw
//!             unreachable!()
//!         })
//!         .end(|_ctx| async move {
//!             throw!(StatusCode::IM_A_TEAPOT, "I'm a teapot!")
//!         })
//!         .listen("127.0.0.1:8000", |addr| {
//!             info!("Server is listening on {}", addr)
//!         })?
//!         .await?;
//!     Ok(())
//! }
//! ```
//!
//! #### error_handler
//! App has an error_handler to handle error thrown by the top middleware.
//! This is the error_handler:
//!
//! ```rust,no_run
//! use roa_core::{Context, Error, Result, Model, ErrorKind};
//! pub async fn error_handler<M: Model>(context: Context<M>, err: Error) -> Result {
//!     // set status code to err.status_code.
//!     context.resp_mut().await.status = err.status_code;
//!     if err.expose {
//!         // write err.message to response body if err.expose.
//!         context.resp_mut().await.write_str(&err.message);
//!     }
//!     if err.kind == ErrorKind::ServerError {
//!         // thrown to hyper
//!         Err(err)
//!     } else {
//!         // caught
//!         Ok(())
//!     }
//! }
//! ```
//!
//! The error thrown by this error_handler will be handled by hyper.
//!
//! ### Router.
//! Roa provides a configurable and nestable router.
//!
//! ```rust,no_run
//! use roa::preload::*;
//! use roa::router::Router;
//! use roa::core::App;
//! use async_std::task::spawn;
//! use log::info;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let mut router = Router::<()>::new();
//!     // get dynamic "/:id"
//!     router.get("/:id", |ctx| async move {
//!         let id: u64 = ctx.must_param("id").await?.parse()?;
//!         // do something
//!         Ok(())
//!     });
//!     App::new(())
//!         // route with prefix "/user"
//!         .gate(router.routes("/user")?)
//!         .listen("127.0.0.1:8000", |addr| {
//!             info!("Server is listening on {}", addr)
//!         })?
//!         .await?;
//!     
//!     // get "/user/1", then id == 1.
//!     Ok(())
//! }
//! ```
//!
//! ### Query
//!
//! Roa provides a middleware `query_parser`.
//!
//! ```rust,no_run
//! use roa::preload::*;
//! use roa::query::query_parser;
//! use roa::core::App;
//! use async_std::task::spawn;
//! use log::info;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     App::new(())
//!         .gate(query_parser)
//!         .end( |ctx| async move {
//!             let id: u64 = ctx.must_query("id").await?.parse()?;
//!             Ok(())
//!         })
//!         .listen("127.0.0.1:8080", |addr| {
//!             info!("Server is listening on {}", addr)
//!         })?
//!         .await?;     
//!     // request "/?id=1", then id == 1.
//!     Ok(())
//! }
//! ```
//!
//! ### Other modules
//!
//! - body: dealing with body more conviniently.
//! - compress: supports transparent content compression.
//! - cors: CORS support.
//! - forward: "X-Forwarded-*" parser.
//! - header: dealing with headers more conviniently.
//! - jwt: json web token support.
//! - logger: a logger middleware.

#![warn(missing_docs)]

pub use roa_core as core;
pub mod cors;
pub mod forward;
pub mod header;
pub mod logger;
pub mod query;

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

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

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

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

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

/// Reexport all extensional traits.
pub mod preload {
    pub use crate::forward::Forward;
    pub use crate::header::FriendlyHeaders;
    pub use crate::query::Query;

    #[cfg(feature = "body")]
    pub use crate::body::PowerBody;

    #[cfg(feature = "cookies")]
    pub use crate::cookie::Cookier;

    #[cfg(feature = "jwt")]
    pub use crate::jwt::JwtVerifier;

    #[cfg(feature = "router")]
    pub use crate::router::RouterParam;
}