Skip to content

Commit c0aa9cf

Browse files
committed
refactor: rename Chain struct to Middlewares
- Improve documentation and fix all doc examples - MSRV 1.56.0 - Update dependencies - Addci pipelines
1 parent c0b80a2 commit c0aa9cf

File tree

11 files changed

+334
-111
lines changed

11 files changed

+334
-111
lines changed

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: joseluisq
2+
custom: paypal.me/joseluisqs

.github/workflows/audit.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: audit
2+
on:
3+
schedule:
4+
- cron: '20 01 * * *' # Every day at 01:20 UTC
5+
push:
6+
branches:
7+
- master
8+
paths:
9+
- "**/Cargo.lock"
10+
- "**/Cargo.toml"
11+
pull_request:
12+
branches:
13+
- master
14+
paths:
15+
- "**/Cargo.lock"
16+
- "**/Cargo.toml"
17+
18+
jobs:
19+
audit:
20+
runs-on: ubuntu-22.04
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 1
25+
26+
- uses: actions-rs/audit-check@v1
27+
with:
28+
token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/devel.yml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: devel
2+
on:
3+
pull_request:
4+
paths:
5+
- .github/workflows/devel.yml
6+
- .cargo/config.toml
7+
- Cargo.lock
8+
- Cargo.toml
9+
- src/**
10+
push:
11+
branches:
12+
- master
13+
- staging
14+
- trying
15+
paths:
16+
- .github/workflows/devel.yml
17+
- Cargo.lock
18+
- Cargo.toml
19+
- src/**
20+
schedule:
21+
- cron: '15 01 * * *' # Every day at 01:15 UTC
22+
23+
jobs:
24+
test:
25+
name: test
26+
runs-on: ${{ matrix.os }}
27+
env:
28+
# Cargo binary
29+
CARGO_BIN: cargo
30+
# When CARGO_BIN is set to CROSS, this is set to `--target matrix.target`
31+
TARGET_FLAGS: ""
32+
# When CARGO_BIN is set to CROSS, TARGET_DIR includes matrix.target
33+
TARGET_DIR: ./target
34+
# Emit backtraces on panics
35+
RUST_BACKTRACE: 1
36+
# Skip tests
37+
SKIP_TESTS: ""
38+
strategy:
39+
matrix:
40+
build:
41+
- pinned
42+
- linux-musl
43+
- linux-gnu
44+
- macos
45+
- windows-msvc
46+
include:
47+
# Specific Rust channels.
48+
# We test against the latest and minimum Rust stable version.
49+
- build: pinned
50+
os: ubuntu-22.04
51+
rust: 1.70.0
52+
# Some of our release builds are generated by a nightly compiler to take
53+
# advantage of the latest optimizations/compile time improvements.
54+
- build: linux-musl
55+
os: ubuntu-22.04
56+
rust: stable
57+
target: x86_64-unknown-linux-musl
58+
- build: linux-gnu
59+
os: ubuntu-22.04
60+
rust: stable
61+
target: x86_64-unknown-linux-gnu
62+
- build: macos
63+
os: macos-12
64+
rust: stable
65+
target: x86_64-apple-darwin
66+
- build: windows-msvc
67+
os: windows-2022
68+
rust: stable
69+
target: x86_64-pc-windows-msvc
70+
71+
steps:
72+
- name: Checkout repository
73+
uses: actions/checkout@v4
74+
with:
75+
fetch-depth: 1
76+
77+
- name: Install Rust
78+
uses: dtolnay/rust-toolchain@stable
79+
with:
80+
toolchain: ${{ matrix.rust }}
81+
target: ${{ matrix.target }}
82+
83+
- name: Set up Cross
84+
if: ${{ !contains(matrix.os, 'windows') && matrix.target != '' }}
85+
shell: bash
86+
run: |
87+
target=''
88+
case "${{ matrix.os }}" in
89+
*macos*)
90+
target=x86_64-apple-darwin
91+
;;
92+
*)
93+
target=x86_64-unknown-linux-musl
94+
;;
95+
esac
96+
97+
echo "Installing cross..."
98+
curl -sSL \
99+
"https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-$target.tar.gz" \
100+
| sudo tar zxf - -C /usr/local/bin/ cross cross-util
101+
cross -V
102+
echo "CARGO_BIN=/usr/local/bin/cross" >> $GITHUB_ENV
103+
104+
- name: Setup Cargo
105+
shell: bash
106+
run: |
107+
if [[ "${{ matrix.target }}" != "" ]]; then
108+
echo "TARGET_FLAGS=--target=${{ matrix.target }}" >> $GITHUB_ENV
109+
echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV
110+
fi
111+
echo "cargo command is: ${{ env.CARGO_BIN }}"
112+
echo "target flag is: ${{ env.TARGET_FLAGS }}"
113+
echo "target dir is: ${{ env.TARGET_DIR }}"
114+
115+
- name: Run tests
116+
shell: bash
117+
run: |
118+
${{ env.CARGO_BIN }} test --verbose ${{ env.TARGET_FLAGS }} ${{ env.SKIP_TESTS }}
119+
120+
- name: Run build
121+
shell: bash
122+
run: |
123+
${{ env.CARGO_BIN }} build --example server --verbose ${{ env.TARGET_FLAGS }}
124+
125+
checks:
126+
name: checks
127+
runs-on: ubuntu-22.04
128+
129+
steps:
130+
- name: Checkout repository
131+
uses: actions/checkout@v4
132+
with:
133+
fetch-depth: 1
134+
135+
- name: Install stable toolchain
136+
uses: dtolnay/rust-toolchain@stable
137+
with:
138+
toolchain: stable
139+
components: rustfmt, clippy
140+
141+
- name: Check formatting
142+
run: |
143+
cargo fmt --all -- --check
144+
145+
- name: Check via Clippy
146+
run: |
147+
cargo clippy --all-features -- -D warnings
148+
149+
- name: Check crate docs
150+
run: |
151+
cargo doc --lib --no-deps

Cargo.toml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ authors = ["Jose Quintana <https://joseluisq.net>"]
66
license = "MIT OR Apache-2.0"
77
repository = "https://github.com/static-web-server/hyper-middleware"
88
documentation = "https://docs.rs/hyper-middleware"
9-
edition = "2018"
9+
edition = "2021"
10+
rust-version = "1.56.0"
1011
categories = ["network-programming", "web-programming::http-server"]
1112
include = [
1213
"src/**/*.rs",
@@ -25,12 +26,22 @@ keywords = [
2526
]
2627

2728
[dependencies]
28-
hyper = { version = "0.14.27", default-features = false, features = ["server", "tcp"] }
29-
anyhow = "1.0.75"
30-
thiserror = "1.0.50"
31-
async-trait = "0.1.74"
29+
hyper = { version = "0.14.28", default-features = false, features = ["server", "tcp"] }
30+
anyhow = "1.0.79"
31+
thiserror = "1.0.56"
32+
async-trait = "0.1.77"
3233
async-recursion = "1.0.5"
3334

3435
[dev-dependencies]
3536
hyper = { version = "0.14", features = ["tcp", "server", "http1"] }
3637
tokio = { version = "1", features = ["rt-multi-thread", "macros"], default-features = false }
38+
39+
[profile.release]
40+
codegen-units = 1
41+
debug = false
42+
debug-assertions = false
43+
lto = "fat"
44+
opt-level = 3
45+
panic = "abort"
46+
rpath = false
47+
strip = true

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
- Compact Middleware and Handler System inspired by [The Iron Framework](https://github.com/iron/iron).
1313
- Simple [Hyper Service](https://docs.rs/hyper/latest/hyper/service/trait.Service.html) with convenient __Remote Address__ access.
1414
- Convenient `Error` and `Result` types powered by [anyhow](https://github.com/dtolnay/anyhow).
15+
- `Async` support via [async-trait](https://github.com/dtolnay/async-trait).
16+
- Macros to facilitate HTTP response errors or error casting.
1517

1618
## Example
1719

1820
[examples/server.rs](examples/server.rs)
1921

2022
```rust
21-
#![deny(warnings)]
22-
2323
use hyper::{header, Server, StatusCode};
24-
use std::{net::SocketAddr, path::PathBuf};
2524
use hyper_middleware::{
26-
async_trait, AfterMiddleware, BeforeMiddleware, Body, Chain, Error, Handler, Request, Response,
27-
Result, Service,
25+
async_trait, AfterMiddleware, BeforeMiddleware, Body, Error, Handler, Middlewares, Request,
26+
Response, Result, Service,
2827
};
28+
use std::{net::SocketAddr, path::PathBuf};
2929

3030
struct Config {
3131
pub root: PathBuf,
@@ -39,17 +39,18 @@ struct Application {
3939
impl Handler for Application {
4040
async fn handle(&self, req: &mut Request) -> Result<Response> {
4141
// Access the Hyper incoming Request
42-
println!("Handler - URI Path: {}", req.uri().path());
42+
println!("Application::handle() - URI Path: {}", req.uri().path());
4343

4444
// Access the custom app options
45-
println!("Config Root: {}", self.opts.root.display());
46-
47-
// Access the Remote Address
4845
println!(
49-
"Remote Addr: {}",
50-
req.extensions().get::<SocketAddr>().unwrap()
46+
"Application::handle() - Config Root: {}",
47+
self.opts.root.display()
5148
);
5249

50+
// Access the Remote Address
51+
let remote_addr = req.extensions().get::<SocketAddr>().unwrap();
52+
println!("Application::handle() - Remote Addr: {}", remote_addr);
53+
5354
// Create a Hyper Response and send it back to the middlewares chain
5455
Ok(Response::new(Body::from("¡Hola!")))
5556
}
@@ -60,10 +61,10 @@ struct FirstMiddleware {}
6061
#[async_trait]
6162
impl BeforeMiddleware for FirstMiddleware {
6263
async fn before(&self, req: &mut Request) -> Result {
63-
println!("First Middleware called!");
64+
println!("FirstMiddleware::before()");
6465

6566
// Access the Hyper incoming Request
66-
println!("First - URI Path: {}", req.uri().path());
67+
println!("FirstMiddleware::before() - URI Path: {}", req.uri().path());
6768

6869
Ok(())
6970
}
@@ -78,7 +79,7 @@ struct SecondMiddleware {}
7879
#[async_trait]
7980
impl AfterMiddleware for SecondMiddleware {
8081
async fn after(&self, _: &mut Request, mut res: Response) -> Result<Response> {
81-
println!("Second Middleware called!");
82+
println!("SecondMiddleware::after()");
8283

8384
// Mutate the Hyper Response at convenience
8485
// and send it back to other middlewares on the chain
@@ -105,17 +106,15 @@ async fn main() -> Result {
105106
root: std::env::current_dir().unwrap(),
106107
};
107108

108-
// 1. Create a custom middleware chain
109-
let mut handler = Chain::new(Application { opts });
109+
// 1. Create a custom middleware chain and plug in some custom middlewares
110+
let mut middlewares = Middlewares::new(Application { opts });
111+
middlewares.link_before(FirstMiddleware {});
112+
middlewares.link_after(SecondMiddleware {});
110113

111-
// 2. Plug in some custom middlewares
112-
handler.link_before(FirstMiddleware {});
113-
handler.link_after(SecondMiddleware {});
114+
// 2. Create a Hyper service and set the current handler with its middlewares
115+
let service = Service::new(middlewares);
114116

115-
// 3. Create a Hyper service and set the current handler with its middlewares
116-
let service = Service::new(handler);
117-
118-
// 4. Finally just run server using the service already created
117+
// 3. Finally just run server using the service already created
119118
let addr = ([127, 0, 0, 1], 8787).into();
120119
let server = Server::bind(&addr).serve(service);
121120

@@ -129,7 +128,7 @@ async fn main() -> Result {
129128

130129
To run the example just type:
131130

132-
```
131+
```sh
133132
cargo run --example server
134133
```
135134

0 commit comments

Comments
 (0)