Skip to content

Commit 07f5920

Browse files
committed
docs: improve api descriptions/examples
1 parent 1de90df commit 07f5920

File tree

4 files changed

+77
-29
lines changed

4 files changed

+77
-29
lines changed

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
//! # hyper_middleware
44
//!
55
//! `hyper_middleware` is a complement set of HTTP middlewares with a small Hyper Service to get started with a HTTP server easily.
6-
//! This module only supports Hyper `0.14.x`.
6+
//! This module only supports Hyper `0.14`.
77
//!
88
//! ## Features
99
//!
10-
//! - Compact Middleware and Handler System inspired by [The Iron Framework](https://github.com/iron/iron).
11-
//! - Simple [Hyper Service](https://docs.rs/hyper/latest/hyper/service/trait.Service.html) with convenient __Remote Address__ access.
12-
//! - Convenient `Error` and `Result` types powered by [anyhow](https://github.com/dtolnay/anyhow).
10+
//! - Compact [Middleware & Handler System][`middleware`] inspired by [The Iron Framework](https://github.com/iron/iron).
11+
//! - Simple [Hyper Service][`hyper::service::Service`] with [Remote Address][`hyper::server::conn::AddrStream`] access.
12+
//! - Convenient [`Error`] and [`Result`] types powered by [anyhow](https://github.com/dtolnay/anyhow).
13+
//!
1314
14-
mod middleware;
15-
mod service;
16-
mod types;
15+
pub mod middleware;
16+
pub mod service;
17+
pub mod types;
1718

1819
pub use middleware::*;
1920
pub use service::*;

src/middleware.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
//! The Iron middleware and handler system for latest Hyper `0.14.x`.
2-
//! It is borrowed from the Iron project and adapted at convenience.
1+
//! The middleware and handler system module.
32
//!
4-
//! <https://github.com/iron/iron/blob/master/iron/src/middleware/mod.rs>
3+
//! It's highly inspired by [The Iron middleware & handler system](https://github.com/iron/iron/blob/master/iron/src/middleware/mod.rs) and intended to work with latest [Hyper][`hyper`] `0.14`.
54
//!
6-
//! The Iron middleware and handler system are the fundamental building blocks
5+
//! The middleware and handler system are the fundamental building blocks
76
//! for handling HTTP requests and generating responses with Hyper.
87
//!
9-
//! This module is intended to work with latest Hyper `0.14.x`.
10-
//!
118
//! # Handlers
129
//!
1310
//! A `Handler` will produce a `Response` given a `Request`. Most handlers are
@@ -19,7 +16,7 @@
1916
//! Here's an example of a `Handler`:
2017
//!
2118
//! ```rust
22-
//! use hyper_middleware::{Request, Response};
19+
//! use hyper_middleware::{Request, Response, Result};
2320
//!
2421
//! fn hello_handler(req: &mut Request) -> Result<Response> {
2522
//! Ok(Response::builder().body(Body::from("¡Hola!")).unwrap())
@@ -56,32 +53,41 @@
5653
//!
5754
//! ```rust
5855
//! use hyper::Server;
59-
//! use hyper_middleware::{Request, Response, RouterService};
60-
//!
61-
//! fn hello_handler(req: &mut Request) -> Result<Response> {
62-
//! Ok(Response::builder().body(Body::from("¡Hola!")).unwrap())
63-
//! };
56+
//! use hyper_middleware::{BeforeMiddleware, Body, Chain, Request, Response, Result, Service};
57+
//!
58+
//! fn hello_handler(_req: &mut Request) -> Result<Response> {
59+
//! let mut resp = Response::new(Body::from("¡Hola!"));
60+
//! resp.headers_mut().insert(
61+
//! header::CONTENT_TYPE,
62+
//! "text/html; charset=utf-8".parse().unwrap(),
63+
//! );
64+
//! Ok(resp)
65+
//! }
6466
//!
65-
//! struct RequestLoggingMiddleware {};
67+
//! struct RequestLoggingMiddleware {}
6668
//! impl BeforeMiddleware for RequestLoggingMiddleware {
67-
//! fn before(&self, req: &mut Request) -> Result<()> {
69+
//! fn before(&self, req: &mut Request) -> Result {
6870
//! println!("{:?}", req);
6971
//! Ok(())
7072
//! }
7173
//! }
7274
//!
75+
//! #[tokio::main(flavor = "multi_thread")]
7376
//! async fn main() -> Result {
7477
//! let mut chain = Chain::new(hello_handler);
78+
//! // Plug in the custom middleware(s)
7579
//! chain.link_before(RequestLoggingMiddleware {});
7680
//!
7781
//! let addr = ([127, 0, 0, 1], 8787).into();
78-
//! let service = RouterService::new(chain);
79-
82+
//! let service = Service::new(chain);
8083
//! let server = Server::bind(&addr).serve(service);
8184
//! println!("Listening on http://{}", addr);
8285
//!
8386
//! server.await?;
87+
//!
88+
//! Ok(())
8489
//! }
90+
8591
//! ```
8692
//!
8793
//! # The Request Handling Flow

src/service.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
//! The Hyper service module.
2+
//!
3+
//! It provides a [Hyper Service][`hyper::service::Service`] implementation intended to work with
4+
//! the [Hyper Server Builder][`hyper::server::Builder`].
5+
//!
6+
//! The service allows to bind a [`Chain`][`super::Chain`] of middlewares.
7+
//!
8+
//! ## Example
9+
//!
10+
//! ```rust
11+
//! use hyper::Server;
12+
//! use hyper_middleware::{
13+
//! Body, Handler, Request, Response, Result, Service
14+
//! };
15+
//!
16+
//! struct Application {}
17+
//! impl Handler for Application {
18+
//! fn handle(&self, _req: &mut Request) -> Result<Response> {
19+
//! // Create a response and send it back to the middlewares chain
20+
//! Ok(Response::new(Body::from("¡Hola!")))
21+
//! }
22+
//! }
23+
//!
24+
//! #[tokio::main(flavor = "multi_thread")]
25+
//! async fn main() -> Result {
26+
//! let mut my_handler = Chain::new(Application {});
27+
//!
28+
//! let my_service = Service::new(my_handler);
29+
//!
30+
//! let addr = ([127, 0, 0, 1], 8787).into();
31+
//! let server = Server::bind(&addr).serve(my_service);
32+
//!
33+
//! println!("Listening on http://{}", addr);
34+
//!
35+
//! server.await?;
36+
//! Ok(())
37+
//! }
38+
//! ```
39+
140
use hyper::server::conn::AddrStream;
241
use hyper::service::Service as HyperService;
342
use std::convert::Infallible;
@@ -7,7 +46,7 @@ use std::task::{Context, Poll};
746
use self::handler_service::{HandlerService, HandlerServiceBuilder};
847
use crate::middleware::Handler;
948

10-
/// Hyper Service entry point which hosts a `Handler`.
49+
/// A [Hyper Service][`hyper::service::Service`] entry point which hosts a [`Handler`].
1150
pub struct Service<H> {
1251
builder: HandlerServiceBuilder<H>,
1352
}

src/types.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
/// A `hyper::Body` type alias.
1+
//! Set of types aliases for convenience.
2+
3+
/// A [`hyper::Body`] type alias.
24
pub type Body = hyper::Body;
35

4-
/// A `hyper::Request<Body>` type alias.
6+
/// A [`hyper::Request<Body>`] type alias.
57
pub type Request<T = Body> = hyper::Request<T>;
68

7-
/// A `hyper::Response<Body>` type alias.
9+
/// A [`hyper::Response<Body>`] type alias.
810
pub type Response<T = Body> = hyper::Response<T>;
911

10-
/// An `anyhow::Error` type alias.
12+
/// An [`anyhow::Error`] type alias.
1113
pub type Error = anyhow::Error;
1214

13-
/// An `anyhow::Result` type alias.
15+
/// An [`anyhow::Result`] type alias.
1416
pub type Result<T = (), E = Error> = anyhow::Result<T, E>;

0 commit comments

Comments
 (0)