Documented, ready for release almost

Signed-off-by: Erik Hollensbe <linux@hollensbe.org>
This commit is contained in:
Erik Hollensbe
2022-01-22 11:50:39 -08:00
parent 2922b3136c
commit 78a434af99
4 changed files with 40 additions and 8 deletions
+1
View File
@@ -47,6 +47,7 @@ use crate::{handler::Handler, router::Router, Error, ServerError};
/// Variadic path components are accessible through the [crate::Params] implementation. Paths are
/// typically used through [crate::app::App] methods that use a string form of the Path.
///
/// Requests are routed through paths to [crate::handler::HandlerFunc]s.
#[derive(Clone)]
pub struct App<S: Clone + Send> {
router: Router<S>,
+30 -1
View File
@@ -7,7 +7,29 @@ use http::{Request, Response};
use hyper::Body;
/// HandlerFunc is the type signature of all handlers. All handlers must conform to this pattern to
/// be used with `compose_handler!`.
/// be used with [crate::compose_handler!].
///
/// Example:
///
/// ```
/// use ratpack::prelude::*;
///
/// async fn hello(
/// req: Request<Body>,
/// _resp: Option<Response<Body>>,
/// params: Params,
/// _app: App<()>,
/// ) -> HTTPResult {
/// let name = params.get("name").unwrap();
/// let bytes = Body::from(format!("hello, {}!\n", name));
///
/// return Ok((
/// req,
/// Some(Response::builder().status(200).body(bytes).unwrap()),
/// ));
/// }
/// ```
///
pub type HandlerFunc<S> = fn(
req: Request<Body>,
response: Option<Response<Body>>,
@@ -15,6 +37,10 @@ pub type HandlerFunc<S> = fn(
app: App<S>,
) -> PinBox<dyn Future<Output = HTTPResult> + Send>;
/// Handler is the structure of the handler. Typically, you will not use this directly, and instead
/// interact with the [crate::compose_handler!] macro. That said, if you wanted to define your own
/// macros or otherwise compose more complicated structures for your handlers, this is available to
/// you.
#[derive(Clone)]
pub struct Handler<S: Clone + Send> {
handler: HandlerFunc<S>,
@@ -26,6 +52,8 @@ where
Self: Send,
S: Clone + Send,
{
/// Construct a new handler composed of a HandlerFunc with state, and an optional next handler
/// in the chain.
pub fn new(handler: HandlerFunc<S>, next: Option<Handler<S>>) -> Self {
Self {
handler,
@@ -33,6 +61,7 @@ where
}
}
/// Perform the function, this will recursively execute all handlers in the chain.
#[async_recursion]
pub async fn perform(
&self,
+3 -1
View File
@@ -71,7 +71,9 @@ where
/// HTTPResult is the return type for handlers. If a handler terminates at the end of its chain
/// with [std::option::Option::None] as the [http::Response], a 500 Internal Server Error will be
/// returned.
/// returned. If you wish to return Err(), a [http::StatusCode] or [std::string::String] can be
/// returned, the former is resolved to its status with an empty body, and the latter corresponds
/// to a 500 Internal Server Error with the body set to the string.
pub type HTTPResult = Result<(Request<hyper::Body>, Option<Response<hyper::Body>>), Error>;
/// A convenience import to gather all of `ratpack`'s dependencies in one easy place.
+6 -6
View File
@@ -1,9 +1,9 @@
/// compose_handler allows you to combine [crate::handler::HandlerFunc] functions into a single [crate::handler::Handler], so that
/// they cascade through a chain of responsibility. This means that each handler will feed its
/// output into the input of the next. To start, the first [http::Response] is
/// [std::option::Option::None], and the final return Response must be non-None otherwise a 500
/// Internal Server Error is returned. Handlers may do anything they wish to the [http::Request] between
/// processing periods, including replacing the request entirely.
/// compose_handler allows you to combine multiple [crate::handler::HandlerFunc] functions into a
/// single [crate::handler::Handler], so that they cascade through a chain of responsibility. This
/// means that each handler will feed its output into the input of the next. To start, the first
/// [http::Response] is [std::option::Option::None], and the final return Response must be
/// non-None; otherwise a 500 Internal Server Error is returned. Handlers may do anything they wish
/// to the [http::Request] between processing periods, including replacing the request entirely.
#[macro_export]
macro_rules! compose_handler {
($( $x:path ),*) => {