Skip to content

Commit 51119d3

Browse files
committed
chore: improve code example
1 parent c23d3d2 commit 51119d3

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ hyper = { version = "0.14", default-features = false, features = ["server", "tcp
2727
anyhow = "1.0"
2828

2929
[dev-dependencies]
30-
# hyper = { version = "0.14", features = ["tcp", "server", "http1"] }
30+
hyper = { version = "0.14", features = ["tcp", "server", "http1"] }
3131
tokio = { version = "1", features = ["rt-multi-thread", "macros"], default-features = false }

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Hyper Middleware
1+
# Hyper Middleware (WIP)
22

33
> A compact middleware and handler system for [Hyper](https://github.com/hyperium/hyper) `0.14.x`.
44
@@ -10,12 +10,12 @@
1010

1111
## Example
1212

13-
[src/bin/server.rs](src/bin/server.rs)
13+
[examples/server.rs](examples/server.rs)
1414

1515
```rust
1616
#![deny(warnings)]
1717

18-
use hyper::{Server, StatusCode};
18+
use hyper::{header, Server, StatusCode};
1919
use std::{net::SocketAddr, path::PathBuf};
2020

2121
use hyper_middleware::{
@@ -33,10 +33,10 @@ struct Application {
3333

3434
impl Handler for Application {
3535
fn handle(&self, req: &mut Request) -> Result<Response> {
36-
// Access Hyper incoming Request
36+
// Access the Hyper incoming Request
3737
println!("URI Path: {}", req.uri().path());
3838

39-
// Access custom app options
39+
// Access the custom app options
4040
println!("Config Root: {}", self.opts.root.display());
4141

4242
// Access the Remote Address
@@ -45,16 +45,20 @@ impl Handler for Application {
4545
req.extensions().get::<SocketAddr>().unwrap()
4646
);
4747

48-
// Send a response back to the middlewares chain
48+
// Create an Hyper Response and send it back to the middlewares chain
4949
Ok(Response::builder().body(Body::from("¡Hola!")).unwrap())
5050
}
5151
}
5252

5353
struct FirstMiddleware {}
5454

5555
impl BeforeMiddleware for FirstMiddleware {
56-
fn before(&self, _: &mut Request) -> Result<()> {
56+
fn before(&self, req: &mut Request) -> Result<()> {
5757
println!("First Middleware called!");
58+
59+
// Access the Hyper incoming Request
60+
println!("URI Path: {}", req.uri().path());
61+
5862
Ok(())
5963
}
6064

@@ -66,8 +70,16 @@ impl BeforeMiddleware for FirstMiddleware {
6670
struct SecondMiddleware {}
6771

6872
impl AfterMiddleware for SecondMiddleware {
69-
fn after(&self, _: &mut Request, res: Response) -> Result<Response> {
73+
fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
7074
println!("Second Middleware called!");
75+
76+
// Mutate the Hyper Response at convenience
77+
// and send it back to other middlewares on the chain
78+
res.headers_mut().insert(
79+
header::CONTENT_TYPE,
80+
"text/html; charset=utf-8".parse().unwrap(),
81+
);
82+
7183
Ok(res)
7284
}
7385

@@ -93,7 +105,7 @@ async fn main() -> Result {
93105
handler.link_before(FirstMiddleware {});
94106
handler.link_after(SecondMiddleware {});
95107

96-
// 3. Create a Hyper service and set the current handler with its middlewares
108+
// 3. Create an Hyper service and set the current handler with its middlewares
97109
let service = Service::new(handler);
98110

99111
// 4. Finally just run server using the service already created
@@ -108,6 +120,12 @@ async fn main() -> Result {
108120
}
109121
```
110122

123+
To run the example just type:
124+
125+
```
126+
cargo run --example server
127+
```
128+
111129
## Contributions
112130

113131
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.

examples/server.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![deny(rust_2018_idioms)]
44
#![deny(dead_code)]
55

6-
use hyper::{Server, StatusCode};
6+
use hyper::{header, Server, StatusCode};
77
use std::{net::SocketAddr, path::PathBuf};
88

99
use hyper_middleware::{
@@ -21,10 +21,10 @@ struct Application {
2121

2222
impl Handler for Application {
2323
fn handle(&self, req: &mut Request) -> Result<Response> {
24-
// Access Hyper incoming Request
24+
// Access the Hyper incoming Request
2525
println!("URI Path: {}", req.uri().path());
2626

27-
// Access custom app options
27+
// Access the custom app options
2828
println!("Config Root: {}", self.opts.root.display());
2929

3030
// Access the Remote Address
@@ -33,16 +33,20 @@ impl Handler for Application {
3333
req.extensions().get::<SocketAddr>().unwrap()
3434
);
3535

36-
// Send a response back to the middlewares chain
36+
// Create an Hyper Response and send it back to the middlewares chain
3737
Ok(Response::builder().body(Body::from("¡Hola!")).unwrap())
3838
}
3939
}
4040

4141
struct FirstMiddleware {}
4242

4343
impl BeforeMiddleware for FirstMiddleware {
44-
fn before(&self, _: &mut Request) -> Result<()> {
44+
fn before(&self, req: &mut Request) -> Result<()> {
4545
println!("First Middleware called!");
46+
47+
// Access the Hyper incoming Request
48+
println!("URI Path: {}", req.uri().path());
49+
4650
Ok(())
4751
}
4852

@@ -54,8 +58,16 @@ impl BeforeMiddleware for FirstMiddleware {
5458
struct SecondMiddleware {}
5559

5660
impl AfterMiddleware for SecondMiddleware {
57-
fn after(&self, _: &mut Request, res: Response) -> Result<Response> {
61+
fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
5862
println!("Second Middleware called!");
63+
64+
// Mutate the Hyper Response at convenience
65+
// and send it back to other middlewares on the chain
66+
res.headers_mut().insert(
67+
header::CONTENT_TYPE,
68+
"text/html; charset=utf-8".parse().unwrap(),
69+
);
70+
5971
Ok(res)
6072
}
6173

@@ -81,7 +93,7 @@ async fn main() -> Result {
8193
handler.link_before(FirstMiddleware {});
8294
handler.link_after(SecondMiddleware {});
8395

84-
// 3. Create a Hyper service and set the current handler with its middlewares
96+
// 3. Create an Hyper service and set the current handler with its middlewares
8597
let service = Service::new(handler);
8698

8799
// 4. Finally just run server using the service already created

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![deny(missing_docs)]
1+
// TODO: #![deny(missing_docs)]
22

33
//! # hyper_middleware
44
//!

0 commit comments

Comments
 (0)