Skip to content

Commit 738047e

Browse files
committed
feat: async_trait support
1 parent 3cbe132 commit 738047e

File tree

7 files changed

+118
-76
lines changed

7 files changed

+118
-76
lines changed

Diff for: Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ keywords = [
2525
]
2626

2727
[dependencies]
28-
hyper = { version = "0.14", default-features = false, features = ["server", "tcp"] }
29-
anyhow = "1.0"
30-
thiserror = "1.0"
28+
hyper = { version = "0.14.25", default-features = false, features = ["server", "tcp"] }
29+
anyhow = "1.0.70"
30+
thiserror = "1.0.40"
31+
async-trait = "0.1.68"
32+
async-recursion = "1.0.4"
3133

3234
[dev-dependencies]
3335
hyper = { version = "0.14", features = ["tcp", "server", "http1"] }

Diff for: Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
docs:
2+
@cargo doc --no-deps
3+
.PHONY: docs
4+
5+
docs-dev:
6+
@cargo doc --no-deps
7+
@echo "Crate documentation: http://localhost:8787/hyper_middleware"
8+
@static-web-server -p 8787 -d target/doc/ \
9+
& watchman-make -p 'src/**/*.rs' --run 'cargo doc'
10+
.PHONY: docs-dev

Diff for: README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
use hyper::{header, Server, StatusCode};
2424
use std::{net::SocketAddr, path::PathBuf};
25-
2625
use hyper_middleware::{
27-
AfterMiddleware, BeforeMiddleware, Body, Chain, Error, Handler, Request, Response, Result,
28-
Service,
26+
async_trait, AfterMiddleware, BeforeMiddleware, Body, Chain, Error, Handler, Request, Response,
27+
Result, Service,
2928
};
3029

3130
struct Config {
@@ -36,8 +35,9 @@ struct Application {
3635
opts: Config,
3736
}
3837

38+
#[async_trait]
3939
impl Handler for Application {
40-
fn handle(&self, req: &mut Request) -> Result<Response> {
40+
async fn handle(&self, req: &mut Request) -> Result<Response> {
4141
// Access the Hyper incoming Request
4242
println!("Handler - URI Path: {}", req.uri().path());
4343

@@ -57,8 +57,9 @@ impl Handler for Application {
5757

5858
struct FirstMiddleware {}
5959

60+
#[async_trait]
6061
impl BeforeMiddleware for FirstMiddleware {
61-
fn before(&self, req: &mut Request) -> Result {
62+
async fn before(&self, req: &mut Request) -> Result {
6263
println!("First Middleware called!");
6364

6465
// Access the Hyper incoming Request
@@ -67,15 +68,16 @@ impl BeforeMiddleware for FirstMiddleware {
6768
Ok(())
6869
}
6970

70-
fn catch(&self, _: &mut Request, err: Error) -> Result {
71+
async fn catch(&self, _: &mut Request, err: Error) -> Result {
7172
Err(err)
7273
}
7374
}
7475

7576
struct SecondMiddleware {}
7677

78+
#[async_trait]
7779
impl AfterMiddleware for SecondMiddleware {
78-
fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
80+
async fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
7981
println!("Second Middleware called!");
8082

8183
// Mutate the Hyper Response at convenience
@@ -88,7 +90,7 @@ impl AfterMiddleware for SecondMiddleware {
8890
Ok(res)
8991
}
9092

91-
fn catch(&self, _: &mut Request, err: Error) -> Result<Response> {
93+
async fn catch(&self, _: &mut Request, err: Error) -> Result<Response> {
9294
Ok(Response::builder()
9395
.status(StatusCode::NOT_FOUND)
9496
.body(Body::from(err.to_string()))

Diff for: examples/server.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use hyper::{header, Server, StatusCode};
77
use std::{net::SocketAddr, path::PathBuf};
88

99
use hyper_middleware::{
10-
AfterMiddleware, BeforeMiddleware, Body, Chain, Error, Handler, Request, Response, Result,
11-
Service,
10+
async_trait, AfterMiddleware, BeforeMiddleware, Body, Chain, Error, Handler, Request, Response,
11+
Result, Service,
1212
};
1313

1414
struct Config {
@@ -19,8 +19,9 @@ struct Application {
1919
opts: Config,
2020
}
2121

22+
#[async_trait]
2223
impl Handler for Application {
23-
fn handle(&self, req: &mut Request) -> Result<Response> {
24+
async fn handle(&self, req: &mut Request) -> Result<Response> {
2425
// Access the Hyper incoming Request
2526
println!("Handler - URI Path: {}", req.uri().path());
2627

@@ -40,8 +41,9 @@ impl Handler for Application {
4041

4142
struct FirstMiddleware {}
4243

44+
#[async_trait]
4345
impl BeforeMiddleware for FirstMiddleware {
44-
fn before(&self, req: &mut Request) -> Result {
46+
async fn before(&self, req: &mut Request) -> Result {
4547
println!("First Middleware called!");
4648

4749
// Access the Hyper incoming Request
@@ -50,15 +52,16 @@ impl BeforeMiddleware for FirstMiddleware {
5052
Ok(())
5153
}
5254

53-
fn catch(&self, _: &mut Request, err: Error) -> Result {
55+
async fn catch(&self, _: &mut Request, err: Error) -> Result {
5456
Err(err)
5557
}
5658
}
5759

5860
struct SecondMiddleware {}
5961

62+
#[async_trait]
6063
impl AfterMiddleware for SecondMiddleware {
61-
fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
64+
async fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
6265
println!("Second Middleware called!");
6366

6467
// Mutate the Hyper Response at convenience
@@ -71,7 +74,7 @@ impl AfterMiddleware for SecondMiddleware {
7174
Ok(res)
7275
}
7376

74-
fn catch(&self, _: &mut Request, err: Error) -> Result<Response> {
77+
async fn catch(&self, _: &mut Request, err: Error) -> Result<Response> {
7578
Ok(Response::builder()
7679
.status(StatusCode::NOT_FOUND)
7780
.body(Body::from(err.to_string()))

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub mod middleware;
1818
pub mod remote_addr;
1919
pub mod service;
2020

21+
pub use async_recursion::*;
22+
pub use async_trait::*;
2123
pub use error::{Context, Error, Result};
2224
pub use http::*;
2325
pub use middleware::*;

0 commit comments

Comments
 (0)