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
//! [![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-core/badge.svg)](https://docs.rs/roa-core)
//! [![Crate version](https://img.shields.io/crates/v/roa-core.svg)](https://crates.io/crates/roa-core)
//! [![Download](https://img.shields.io/crates/d/roa-core.svg)](https://crates.io/crates/roa-core)
//! [![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
//!
//! Core components of Roa framework.
//!
//! If you are new to roa, please go to the documentation of roa framework.
//!
//! ### 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
//! use roa_core::App;
//!
//! let mut app = App::new(());
//! app.end(|mut ctx| async move {
//!     ctx.resp_mut().write_str("Hello, World");
//!     Ok(())
//! });
//! ```
//!
//! #### Cascading
//!
//! The following example responds with "Hello World", however, the request flows through
//! the `logging` middleware to mark when the request started, then continue
//! to yield control through the response middleware. When a middleware invokes `next.await`
//! 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
//! use roa_core::App;
//! use std::time::Instant;
//! use log::info;
//!
//! let mut app = App::new(());
//! app.gate_fn(|_ctx, next| async move {
//!     let inbound = Instant::now();
//!     next.await?;
//!     info!("time elapsed: {} ms", inbound.elapsed().as_millis());
//!     Ok(())
//! });
//!
//! app.end(|mut ctx| async move {
//!     ctx.resp_mut().write_str("Hello, World");
//!     Ok(())
//! });
//! ```
//!
//! ### Error Handling
//!
//! You can catch or straightly throw an Error returned by next.
//!
//! ```rust
//! use roa_core::{App, throw};
//! use roa_core::http::StatusCode;
//!         
//! let mut app = App::new(());
//! app.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(())
//! });
//! app.gate_fn(|ctx, next| async move {
//!     next.await?; // just throw
//!     unreachable!()
//! });
//! app.end(|_ctx| async move {
//!     throw!(StatusCode::IM_A_TEAPOT, "I'm a teapot!")
//! });
//! ```
//!
//! #### error_handler
//! App has an error_handler to handle `Error` thrown by the top middleware.
//! This is the error_handler:
//!
//! ```rust
//! use roa_core::{Context, Error, Result, ErrorKind, State};
//! pub async fn error_handler<S: State>(mut context: Context<S>, err: Error) -> Result {
//!     context.resp_mut().status = err.status_code;
//!     if err.expose {
//!         context.resp_mut().write_str(&err.message);
//!     }
//!     if err.kind == ErrorKind::ServerError {
//!         Err(err)
//!     } else {
//!         Ok(())
//!     }
//! }
//! ```
//!
//! The Error thrown by this error_handler will be handled by hyper.
//!
//! ### HTTP Server.
//!
//! Use roa_core::Server to construct a http server.
//! Please refer to crate roa-tcp for more information.

#![warn(missing_docs)]

mod app;
mod context;
mod err;
mod executor;
mod group;
mod middleware;
mod next;
mod request;
mod response;
mod state;

#[doc(inline)]
pub use app::{AddrStream, App};

#[doc(inline)]
pub use executor::{BlockingObj, Executor, FutureObj, JoinHandle, Spawn};

#[doc(inline)]
pub use context::{Context, SyncContext, Variable};

#[doc(inline)]
pub use err::{Error, ErrorKind, Result, ResultFuture};

#[doc(inline)]
pub use middleware::Middleware;

#[doc(inline)]
pub use group::{join, join_all};

pub use state::State;

#[doc(inline)]
pub use next::{last, Next};

#[doc(inline)]
pub use request::Request;

#[doc(inline)]
pub use response::Response;

pub use http;

pub use hyper::server::{accept::Accept, Server};

pub use async_trait::async_trait;