|
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. |
3 | 2 | //!
|
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`. |
5 | 4 | //!
|
6 |
| -//! The Iron middleware and handler system are the fundamental building blocks |
| 5 | +//! The middleware and handler system are the fundamental building blocks |
7 | 6 | //! for handling HTTP requests and generating responses with Hyper.
|
8 | 7 | //!
|
9 |
| -//! This module is intended to work with latest Hyper `0.14.x`. |
10 |
| -//! |
11 | 8 | //! # Handlers
|
12 | 9 | //!
|
13 | 10 | //! A `Handler` will produce a `Response` given a `Request`. Most handlers are
|
|
19 | 16 | //! Here's an example of a `Handler`:
|
20 | 17 | //!
|
21 | 18 | //! ```rust
|
22 |
| -//! use hyper_middleware::{Request, Response}; |
| 19 | +//! use hyper_middleware::{Request, Response, Result}; |
23 | 20 | //!
|
24 | 21 | //! fn hello_handler(req: &mut Request) -> Result<Response> {
|
25 | 22 | //! Ok(Response::builder().body(Body::from("¡Hola!")).unwrap())
|
|
56 | 53 | //!
|
57 | 54 | //! ```rust
|
58 | 55 | //! 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 | +//! } |
64 | 66 | //!
|
65 |
| -//! struct RequestLoggingMiddleware {}; |
| 67 | +//! struct RequestLoggingMiddleware {} |
66 | 68 | //! impl BeforeMiddleware for RequestLoggingMiddleware {
|
67 |
| -//! fn before(&self, req: &mut Request) -> Result<()> { |
| 69 | +//! fn before(&self, req: &mut Request) -> Result { |
68 | 70 | //! println!("{:?}", req);
|
69 | 71 | //! Ok(())
|
70 | 72 | //! }
|
71 | 73 | //! }
|
72 | 74 | //!
|
| 75 | +//! #[tokio::main(flavor = "multi_thread")] |
73 | 76 | //! async fn main() -> Result {
|
74 | 77 | //! let mut chain = Chain::new(hello_handler);
|
| 78 | +//! // Plug in the custom middleware(s) |
75 | 79 | //! chain.link_before(RequestLoggingMiddleware {});
|
76 | 80 | //!
|
77 | 81 | //! let addr = ([127, 0, 0, 1], 8787).into();
|
78 |
| -//! let service = RouterService::new(chain); |
79 |
| -
|
| 82 | +//! let service = Service::new(chain); |
80 | 83 | //! let server = Server::bind(&addr).serve(service);
|
81 | 84 | //! println!("Listening on http://{}", addr);
|
82 | 85 | //!
|
83 | 86 | //! server.await?;
|
| 87 | +//! |
| 88 | +//! Ok(()) |
84 | 89 | //! }
|
| 90 | +
|
85 | 91 | //! ```
|
86 | 92 | //!
|
87 | 93 | //! # The Request Handling Flow
|
|
0 commit comments