From 21d496ddb1896c5aef34db73c20ea3dcf73ac2cd Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 12 Apr 2023 03:02:19 +1000 Subject: [PATCH 01/15] Removed leftover "path_router hit" debug message (#1925) --- axum/src/routing/mod.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index b7799d7b29..52d204226a 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -299,10 +299,7 @@ where } match self.path_router.call_with_state(req, state) { - Ok(future) => { - println!("path_router hit"); - future - } + Ok(future) => future, Err((mut req, state)) => { let super_fallback = req .extensions_mut() From b92b91e75e2c41ebe9b6bc3c8a715334dff304e1 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 11 Apr 2023 19:38:22 +0200 Subject: [PATCH 02/15] Release axum 0.6.14 (#1926) --- axum/CHANGELOG.md | 6 ++++++ axum/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index b755769893..327d8d5a7e 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - None. +# 0.6.14 (11. April, 2023) + +- **fixed:** Removed leftover "path_router hit" debug message ([#1925]) + +[#1925]: https://github.com/tokio-rs/axum/pull/1925 + # 0.6.13 (11. April, 2023) - **added:** Log rejections from built-in extractors with the diff --git a/axum/Cargo.toml b/axum/Cargo.toml index cb4dcf341f..193b97a89a 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum" -version = "0.6.13" +version = "0.6.14" categories = ["asynchronous", "network-programming", "web-programming::http-server"] description = "Web framework that focuses on ergonomics and modularity" edition = "2021" From 31aed243d9fbed33dec7954336face09b8b31baa Mon Sep 17 00:00:00 2001 From: Andrew Silver Date: Wed, 12 Apr 2023 07:38:21 +1000 Subject: [PATCH 03/15] Remove "NotFound hit" debug message (#1927) --- axum/src/routing/not_found.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/axum/src/routing/not_found.rs b/axum/src/routing/not_found.rs index ffd185bfa1..dc3fec46ac 100644 --- a/axum/src/routing/not_found.rs +++ b/axum/src/routing/not_found.rs @@ -29,7 +29,6 @@ where } fn call(&mut self, _req: Request) -> Self::Future { - println!("NotFound hit"); ready(Ok(StatusCode::NOT_FOUND.into_response())) } } From 8b22b945821cba3f2e2cc633fc61cf8b4c2b3621 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 12 Apr 2023 09:19:00 +0200 Subject: [PATCH 04/15] Remove "PathDeserializationError" debug message --- axum/src/extract/path/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/axum/src/extract/path/mod.rs b/axum/src/extract/path/mod.rs index 0932d9ce14..189e476e5c 100644 --- a/axum/src/extract/path/mod.rs +++ b/axum/src/extract/path/mod.rs @@ -201,7 +201,6 @@ impl PathDeserializationError { #[track_caller] pub(super) fn unsupported_type(name: &'static str) -> Self { - println!("{}", std::panic::Location::caller()); Self::new(ErrorKind::UnsupportedType { name }) } } From cf1ed9bb546354fc075d59cdc02419297f1afe2c Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 12 Apr 2023 09:36:17 +0200 Subject: [PATCH 05/15] Release axum 0.6.15 (#1928) --- axum/CHANGELOG.md | 6 ++++++ axum/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 327d8d5a7e..8ad076d56f 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - None. +# 0.6.15 (12. April, 2023) + +- **fixed:** Removed additional leftover debug messages ([#1927]) + +[#1927]: https://github.com/tokio-rs/axum/pull/1927 + # 0.6.14 (11. April, 2023) - **fixed:** Removed leftover "path_router hit" debug message ([#1925]) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 193b97a89a..c681ad797d 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum" -version = "0.6.14" +version = "0.6.15" categories = ["asynchronous", "network-programming", "web-programming::http-server"] description = "Web framework that focuses on ergonomics and modularity" edition = "2021" From 377d73a55ba7bdbb1a47084c684fb1334b200f1b Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 12 Apr 2023 19:14:16 +0200 Subject: [PATCH 06/15] Add basic merge and nest benchmarks --- axum/benches/benches.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/axum/benches/benches.rs b/axum/benches/benches.rs index 50a7417b3b..c3b9c19e34 100644 --- a/axum/benches/benches.rs +++ b/axum/benches/benches.rs @@ -19,7 +19,20 @@ fn main() { benchmark("minimal").run(Router::new); - benchmark("basic").run(|| Router::new().route("/", get(|| async { "Hello, World!" }))); + benchmark("basic") + .path("/a/b/c") + .run(|| Router::new().route("/a/b/c", get(|| async { "Hello, World!" }))); + + benchmark("basic-merge").path("/a/b/c").run(|| { + let inner = Router::new().route("/a/b/c", get(|| async { "Hello, World!" })); + Router::new().merge(inner) + }); + + benchmark("basic-nest").path("/a/b/c").run(|| { + let c = Router::new().route("/c", get(|| async { "Hello, World!" })); + let b = Router::new().nest("/b", c); + Router::new().nest("/a", b) + }); benchmark("routing").path("/foo/bar/baz").run(|| { let mut app = Router::new(); From 39bb28130dc54ba294b5e6f9af441ba7692d6221 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 14 Apr 2023 22:26:56 +0200 Subject: [PATCH 07/15] Enable `println` warning (#1929) --- .github/workflows/CI.yml | 2 +- axum-core/src/lib.rs | 1 + axum-extra/src/lib.rs | 1 + axum-macros/src/lib.rs | 1 + axum/src/lib.rs | 2 +- examples/websockets/src/client.rs | 1 - 6 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a983ca8665..6fe0e4e54e 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -29,7 +29,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: clippy - args: --workspace --all-targets --all-features + args: --workspace --all-targets --all-features -- -D warnings - name: rustfmt uses: actions-rs/cargo@v1 with: diff --git a/axum-core/src/lib.rs b/axum-core/src/lib.rs index 974e5e18d4..94077c1f1d 100644 --- a/axum-core/src/lib.rs +++ b/axum-core/src/lib.rs @@ -48,6 +48,7 @@ #![allow(elided_lifetimes_in_paths, clippy::type_complexity)] #![forbid(unsafe_code)] #![cfg_attr(test, allow(clippy::float_cmp))] +#![cfg_attr(not(test), warn(clippy::print_stdout, clippy::dbg_macro))] #[macro_use] pub(crate) mod macros; diff --git a/axum-extra/src/lib.rs b/axum-extra/src/lib.rs index a7402f21e8..a964de3753 100644 --- a/axum-extra/src/lib.rs +++ b/axum-extra/src/lib.rs @@ -64,6 +64,7 @@ #![forbid(unsafe_code)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![cfg_attr(test, allow(clippy::float_cmp))] +#![cfg_attr(not(test), warn(clippy::print_stdout, clippy::dbg_macro))] #[allow(unused_extern_crates)] extern crate self as axum_extra; diff --git a/axum-macros/src/lib.rs b/axum-macros/src/lib.rs index 584d83153f..a49383e031 100644 --- a/axum-macros/src/lib.rs +++ b/axum-macros/src/lib.rs @@ -42,6 +42,7 @@ #![forbid(unsafe_code)] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(test, allow(clippy::float_cmp))] +#![cfg_attr(not(test), warn(clippy::print_stdout, clippy::dbg_macro))] use proc_macro::TokenStream; use quote::{quote, ToTokens}; diff --git a/axum/src/lib.rs b/axum/src/lib.rs index b10a8d6bbe..da60aef5ae 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -399,7 +399,6 @@ #![warn( clippy::all, - clippy::dbg_macro, clippy::todo, clippy::empty_enum, clippy::enum_glob_use, @@ -437,6 +436,7 @@ #![forbid(unsafe_code)] #![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))] #![cfg_attr(test, allow(clippy::float_cmp))] +#![cfg_attr(not(test), warn(clippy::print_stdout, clippy::dbg_macro))] #[macro_use] pub(crate) mod macros; diff --git a/examples/websockets/src/client.rs b/examples/websockets/src/client.rs index 2f61ad981b..971b573e50 100644 --- a/examples/websockets/src/client.rs +++ b/examples/websockets/src/client.rs @@ -30,7 +30,6 @@ async fn main() { let start_time = Instant::now(); //spawn several clients that will concurrently talk to the server let mut clients = (0..N_CLIENTS) - .into_iter() .map(|cli| tokio::spawn(spawn_client(cli))) .collect::>(); From 7219fd8df520d295faa42b59f77e25ca2818b6b1 Mon Sep 17 00:00:00 2001 From: tottoto Date: Sat, 15 Apr 2023 16:46:43 +0900 Subject: [PATCH 08/15] Update ci config to setup rust (#1937) --- .github/workflows/CI.yml | 52 ++++++---------------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6fe0e4e54e..31a983243c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -18,11 +18,8 @@ jobs: - uses: arduino/setup-protoc@v1 with: repo-token: ${{ secrets.GITHUB_TOKEN }} - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@beta with: - toolchain: beta - override: true - profile: minimal components: clippy, rustfmt - uses: Swatinem/rust-cache@v2 - name: Check @@ -41,10 +38,6 @@ jobs: steps: - uses: actions/checkout@master - uses: dtolnay/rust-toolchain@stable - with: - toolchain: stable - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: cargo doc env: @@ -57,10 +50,6 @@ jobs: - uses: actions/checkout@master - uses: arduino/setup-protoc@v1 - uses: dtolnay/rust-toolchain@stable - with: - toolchain: stable - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Install cargo-hack run: | @@ -75,11 +64,7 @@ jobs: crate: [axum, axum-core, axum-extra, axum-macros] steps: - uses: actions/checkout@master - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: nightly - override: true - profile: minimal + - uses: dtolnay/rust-toolchain@nightly - uses: Swatinem/rust-cache@v1 - name: Install cargo-public-api-crates run: | @@ -96,11 +81,9 @@ jobs: steps: - uses: actions/checkout@master - uses: arduino/setup-protoc@v1 - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Run tests uses: actions-rs/cargo@v1 @@ -114,12 +97,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@master with: # same as `axum-macros/rust-toolchain` toolchain: nightly-2022-11-18 - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Run nightly tests working-directory: axum-macros @@ -133,16 +114,11 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ env.MSRV }} - override: true - profile: minimal - name: "install Rust nightly" - uses: dtolnay/rust-toolchain@stable - with: - toolchain: nightly - profile: minimal + uses: dtolnay/rust-toolchain@nightly - uses: Swatinem/rust-cache@v1 - name: Select minimal versions uses: actions-rs/cargo@v1 @@ -188,10 +164,6 @@ jobs: steps: - uses: actions/checkout@master - uses: dtolnay/rust-toolchain@stable - with: - toolchain: stable - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Run doc tests uses: actions-rs/cargo@v1 @@ -222,10 +194,7 @@ jobs: - uses: actions/checkout@master - uses: dtolnay/rust-toolchain@stable with: - toolchain: stable target: armv5te-unknown-linux-musleabi - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Check uses: actions-rs/cargo@v1 @@ -250,10 +219,7 @@ jobs: - uses: actions/checkout@master - uses: dtolnay/rust-toolchain@stable with: - toolchain: stable target: wasm32-unknown-unknown - override: true - profile: minimal - uses: Swatinem/rust-cache@v1 - name: Check uses: actions-rs/cargo@v1 @@ -267,11 +233,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@master - - uses: dtolnay/rust-toolchain@stable - with: - toolchain: beta - override: true - profile: minimal + - uses: dtolnay/rust-toolchain@beta - uses: Swatinem/rust-cache@v2 - name: Install cargo-sort run: | From de7dc69eeb938c03a823b1f53b5ec9f68567cf24 Mon Sep 17 00:00:00 2001 From: tottoto Date: Sun, 16 Apr 2023 00:02:47 +0900 Subject: [PATCH 09/15] Resolve ci warnings (#1938) --- .github/workflows/CI.yml | 155 ++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 92 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 31a983243c..4424f90ed1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -14,31 +14,23 @@ jobs: check: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master - - uses: arduino/setup-protoc@v1 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v3 + - uses: taiki-e/install-action@protoc - uses: dtolnay/rust-toolchain@beta with: components: clippy, rustfmt - uses: Swatinem/rust-cache@v2 - name: Check - uses: actions-rs/cargo@v1 - with: - command: clippy - args: --workspace --all-targets --all-features -- -D warnings + run: cargo clippy --workspace --all-targets --all-features -- -D warnings - name: rustfmt - uses: actions-rs/cargo@v1 - with: - command: fmt - args: -- --check + run: cargo fmt --all --check check-docs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: cargo doc env: RUSTDOCFLAGS: "-D rustdoc::all -A rustdoc::private-doc-tests" @@ -47,10 +39,10 @@ jobs: cargo-hack: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master - - uses: arduino/setup-protoc@v1 + - uses: actions/checkout@v3 + - uses: taiki-e/install-action@protoc - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Install cargo-hack run: | curl -LsSf https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-x86_64-unknown-linux-gnu.tar.gz | tar xzf - -C ~/.cargo/bin @@ -63,9 +55,9 @@ jobs: matrix: crate: [axum, axum-core, axum-extra, axum-macros] steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@nightly - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Install cargo-public-api-crates run: | cargo install --git https://github.com/davidpdrsn/cargo-public-api-crates @@ -79,33 +71,29 @@ jobs: matrix: rust: [stable, beta] steps: - - uses: actions/checkout@master - - uses: arduino/setup-protoc@v1 + - uses: actions/checkout@v3 + - uses: taiki-e/install-action@protoc - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run tests - uses: actions-rs/cargo@v1 - with: - command: test - args: --all --all-features --all-targets + run: cargo test --workspace --all-features --all-targets # some examples doesn't support our MSRV so we only test axum itself on our MSRV test-nightly: needs: check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@master with: # same as `axum-macros/rust-toolchain` toolchain: nightly-2022-11-18 - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run nightly tests working-directory: axum-macros - run: | - cargo test + run: cargo test # some examples doesn't support our MSRV (such as async-graphql) # so we only test axum itself on our MSRV @@ -113,63 +101,48 @@ jobs: needs: check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{ env.MSRV }} - name: "install Rust nightly" uses: dtolnay/rust-toolchain@nightly - - uses: Swatinem/rust-cache@v1 - - name: Select minimal versions - uses: actions-rs/cargo@v1 - with: - command: update - args: -Z minimal-versions - toolchain: nightly + - uses: Swatinem/rust-cache@v2 + - name: Select minimal version + run: cargo +nightly update -Z minimal-versions - name: Fix up Cargo.lock - uses: actions-rs/cargo@v1 - with: - command: update - args: -p crc32fast --precise 1.1.1 - toolchain: nightly + run: cargo +nightly update -p crc32fast --precise 1.1.1 - name: Run tests - uses: actions-rs/cargo@v1 - with: - command: test - args: > - -p axum - -p axum-extra - -p axum-core - --all-features - --all-targets - --locked - toolchain: ${{ env.MSRV }} + run: > + cargo +${{ env.MSRV }} + test + -p axum + -p axum-extra + -p axum-core + --all-features + --all-targets + --locked # the compiler errors are different on our MSRV which makes # the trybuild tests in axum-macros fail, so just run the doc # tests - name: Run axum-macros doc tests - uses: actions-rs/cargo@v1 - with: - command: test - args: > - -p axum-macros - --doc - --all-features - --locked - toolchain: ${{ env.MSRV }} + run: > + cargo +${{ env.MSRV }} + test + -p axum-macros + --doc + --all-features + --locked test-docs: needs: check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Run doc tests - uses: actions-rs/cargo@v1 - with: - command: test - args: --all-features --doc + run: cargo test --all-features --doc deny-check: name: cargo-deny check @@ -181,7 +154,7 @@ jobs: - advisories - bans licenses sources steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: EmbarkStudios/cargo-deny-action@v1 with: command: check ${{ matrix.checks }} @@ -191,48 +164,46 @@ jobs: needs: check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable with: target: armv5te-unknown-linux-musleabi - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Check - uses: actions-rs/cargo@v1 env: # Clang has native cross-compilation support CC: clang - with: - command: check - args: > - --all-targets - --all-features - -p axum - -p axum-core - -p axum-extra - -p axum-macros - --target armv5te-unknown-linux-musleabi + run: > + cargo + check + --all-targets + --all-features + -p axum + -p axum-core + -p axum-extra + -p axum-macros + --target armv5te-unknown-linux-musleabi wasm32-unknown-unknown: needs: check runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable with: target: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v1 + - uses: Swatinem/rust-cache@v2 - name: Check - uses: actions-rs/cargo@v1 - with: - command: check - args: > - --manifest-path ./examples/simple-router-wasm/Cargo.toml - --target wasm32-unknown-unknown + run: > + cargo + check + --manifest-path ./examples/simple-router-wasm/Cargo.toml + --target wasm32-unknown-unknown dependencies-are-sorted: runs-on: ubuntu-latest steps: - - uses: actions/checkout@master + - uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@beta - uses: Swatinem/rust-cache@v2 - name: Install cargo-sort From 018d65da1442792a711bc4d43de475121299a1bd Mon Sep 17 00:00:00 2001 From: tottoto Date: Mon, 17 Apr 2023 17:55:18 +0900 Subject: [PATCH 10/15] Use axum-macros/rust-toolchain file in ci (#1939) --- .github/workflows/CI.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 4424f90ed1..62d73cf36e 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -86,10 +86,12 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - name: Get rust-toolchain version + id: rust-toolchain + run: echo "version=$(cat axum-macros/rust-toolchain)" >> $GITHUB_OUTPUT - uses: dtolnay/rust-toolchain@master with: - # same as `axum-macros/rust-toolchain` - toolchain: nightly-2022-11-18 + toolchain: ${{ steps.rust-toolchain.outputs.version }} - uses: Swatinem/rust-cache@v2 - name: Run nightly tests working-directory: axum-macros From e97462d45251c21ffd1ba4fb5db30f3cf6f6cb6c Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 17 Apr 2023 13:20:17 +0200 Subject: [PATCH 11/15] Add `Html`, `Css`, `JavaScript`, and `Wasm` response types (#1921) --- axum-extra/CHANGELOG.md | 7 ++- axum-extra/src/response/mod.rs | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index aaf8bc5b64..59944f0e54 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning]. # Unreleased -- None. +- **added:** Add `Html` response type ([#1921]) +- **added:** Add `Css` response type ([#1921]) +- **added:** Add `JavaScript` response type ([#1921]) +- **added:** Add `Wasm` response type ([#1921]) + +[#1921]: https://github.com/tokio-rs/axum/pull/1921 # 0.7.3 (11. April, 2023) diff --git a/axum-extra/src/response/mod.rs b/axum-extra/src/response/mod.rs index 7926b8c815..8bf9c43cda 100644 --- a/axum-extra/src/response/mod.rs +++ b/axum-extra/src/response/mod.rs @@ -9,3 +9,82 @@ pub use erased_json::ErasedJson; #[cfg(feature = "json-lines")] #[doc(no_inline)] pub use crate::json_lines::JsonLines; + +macro_rules! mime_response { + ( + $(#[$m:meta])* + $ident:ident, + $mime:ident, + ) => { + mime_response! { + $(#[$m])* + $ident, + mime::$mime.as_ref(), + } + }; + + ( + $(#[$m:meta])* + $ident:ident, + $mime:expr, + ) => { + $(#[$m])* + #[derive(Clone, Copy, Debug)] + #[must_use] + pub struct $ident(pub T); + + impl axum::response::IntoResponse for $ident + where + T: axum::response::IntoResponse, + { + fn into_response(self) -> axum::response::Response { + ( + [( + http::header::CONTENT_TYPE, + http::HeaderValue::from_static($mime), + )], + self.0, + ) + .into_response() + } + } + + impl From for $ident { + fn from(inner: T) -> Self { + Self(inner) + } + } + }; +} + +mime_response! { + /// A HTML response. + /// + /// Will automatically get `Content-Type: text/html; charset=utf-8`. + Html, + TEXT_HTML_UTF_8, +} + +mime_response! { + /// A JavaScript response. + /// + /// Will automatically get `Content-Type: application/javascript; charset=utf-8`. + JavaScript, + APPLICATION_JAVASCRIPT_UTF_8, +} + +mime_response! { + /// A CSS response. + /// + /// Will automatically get `Content-Type: text/css; charset=utf-8`. + Css, + TEXT_CSS_UTF_8, +} + +mime_response! { + /// A WASM response. + /// + /// Will automatically get `Content-Type: application/wasm`. + Wasm, + "application/wasm", +} From 2c87d65f179afec253ebedbb11e9031a724ca529 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 17 Apr 2023 13:20:54 +0200 Subject: [PATCH 12/15] Don't allow extracting `MatchedPath` in fallbacks (#1934) --- axum/CHANGELOG.md | 5 +++- axum/src/extract/matched_path.rs | 16 +++++++++++++ axum/src/routing/mod.rs | 6 ++--- axum/src/routing/path_router.rs | 37 +++++++++++++++++------------- axum/src/routing/tests/fallback.rs | 16 +++++++++++++ 5 files changed, 60 insertions(+), 20 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 8ad076d56f..bc0afeb0e6 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **fixed:** Don't allow extracting `MatchedPath` in fallbacks ([#1934]) +- **fixed:** Fix panic if `Router` with something nested at `/` was used as a fallback ([#1934]) + +[#1934]: https://github.com/tokio-rs/axum/pull/1934 # 0.6.15 (12. April, 2023) diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index 57a17bee63..02994c51e0 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -176,6 +176,7 @@ mod tests { Router, }; use http::{Request, StatusCode}; + use hyper::Body; #[crate::test] async fn extracting_on_handler() { @@ -353,4 +354,19 @@ mod tests { let res = client.get("/foo/bar").send().await; assert_eq!(res.status(), StatusCode::OK); } + + #[crate::test] + async fn cant_extract_in_fallback() { + async fn handler(path: Option, req: Request) { + assert!(path.is_none()); + assert!(req.extensions().get::().is_none()); + } + + let app = Router::new().fallback(handler); + + let client = TestClient::new(app); + + let res = client.get("/foo/bar").send().await; + assert_eq!(res.status(), StatusCode::OK); + } } diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index 52d204226a..a1d8c715d9 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -57,8 +57,8 @@ pub(crate) struct RouteId(u32); /// The router type for composing handlers and services. #[must_use] pub struct Router { - path_router: PathRouter, - fallback_router: PathRouter, + path_router: PathRouter, + fallback_router: PathRouter, default_fallback: bool, } @@ -499,7 +499,7 @@ impl fmt::Debug for Endpoint { } } -struct SuperFallback(SyncWrapper>); +struct SuperFallback(SyncWrapper>); #[test] #[allow(warnings)] diff --git a/axum/src/routing/path_router.rs b/axum/src/routing/path_router.rs index 1790b32d88..ca618e7548 100644 --- a/axum/src/routing/path_router.rs +++ b/axum/src/routing/path_router.rs @@ -1,4 +1,4 @@ -use crate::body::{Body, HttpBody}; +use crate::body::HttpBody; use axum_core::response::IntoResponse; use http::Request; use matchit::MatchError; @@ -11,13 +11,13 @@ use super::{ RouteId, NEST_TAIL_PARAM, }; -pub(super) struct PathRouter { +pub(super) struct PathRouter { routes: HashMap>, node: Arc, prev_route_id: RouteId, } -impl PathRouter +impl PathRouter where B: HttpBody + Send + 'static, S: Clone + Send + Sync + 'static, @@ -107,7 +107,10 @@ where Ok(()) } - pub(super) fn merge(&mut self, other: PathRouter) -> Result<(), Cow<'static, str>> { + pub(super) fn merge( + &mut self, + other: PathRouter, + ) -> Result<(), Cow<'static, str>> { let PathRouter { routes, node, @@ -131,7 +134,7 @@ where pub(super) fn nest( &mut self, path: &str, - router: PathRouter, + router: PathRouter, ) -> Result<(), Cow<'static, str>> { let prefix = validate_nest_path(path); @@ -193,7 +196,7 @@ where Ok(()) } - pub(super) fn layer(self, layer: L) -> PathRouter + pub(super) fn layer(self, layer: L) -> PathRouter where L: Layer> + Clone + Send + 'static, L::Service: Service> + Clone + Send + 'static, @@ -250,7 +253,7 @@ where } } - pub(super) fn with_state(self, state: S) -> PathRouter { + pub(super) fn with_state(self, state: S) -> PathRouter { let routes = self .routes .into_iter() @@ -293,12 +296,14 @@ where Ok(match_) => { let id = *match_.value; - #[cfg(feature = "matched-path")] - crate::extract::matched_path::set_matched_path_for_request( - id, - &self.node.route_id_to_path, - req.extensions_mut(), - ); + if !IS_FALLBACK { + #[cfg(feature = "matched-path")] + crate::extract::matched_path::set_matched_path_for_request( + id, + &self.node.route_id_to_path, + req.extensions_mut(), + ); + } url_params::insert_url_params(req.extensions_mut(), match_.params); @@ -347,7 +352,7 @@ where } } -impl Default for PathRouter { +impl Default for PathRouter { fn default() -> Self { Self { routes: Default::default(), @@ -357,7 +362,7 @@ impl Default for PathRouter { } } -impl fmt::Debug for PathRouter { +impl fmt::Debug for PathRouter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("PathRouter") .field("routes", &self.routes) @@ -366,7 +371,7 @@ impl fmt::Debug for PathRouter { } } -impl Clone for PathRouter { +impl Clone for PathRouter { fn clone(&self) -> Self { Self { routes: self.routes.clone(), diff --git a/axum/src/routing/tests/fallback.rs b/axum/src/routing/tests/fallback.rs index ac72de5980..9aa9fbe6aa 100644 --- a/axum/src/routing/tests/fallback.rs +++ b/axum/src/routing/tests/fallback.rs @@ -225,3 +225,19 @@ async fn nest_fallback_on_inner() { assert_eq!(res.status(), StatusCode::NOT_FOUND); assert_eq!(res.text().await, "inner fallback"); } + +// https://github.com/tokio-rs/axum/issues/1931 +#[crate::test] +async fn doesnt_panic_if_used_with_nested_router() { + async fn handler() {} + + let routes_static = + Router::new().nest_service("/", crate::routing::get_service(handler.into_service())); + + let routes_all = Router::new().fallback_service(routes_static); + + let client = TestClient::new(routes_all); + + let res = client.get("/foobar").send().await; + assert_eq!(res.status(), StatusCode::OK); +} From b42897f2dc53baba571a1041606390a55a89d4bb Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 17 Apr 2023 13:41:53 +0200 Subject: [PATCH 13/15] Document that `Router::new().fallback(...)` isn't optimal (#1940) --- axum/CHANGELOG.md | 2 ++ axum/src/docs/routing/fallback.md | 35 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index bc0afeb0e6..b462c03434 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **fixed:** Don't allow extracting `MatchedPath` in fallbacks ([#1934]) - **fixed:** Fix panic if `Router` with something nested at `/` was used as a fallback ([#1934]) +- **added:** Document that `Router::new().fallback(...)` isn't optimal ([#1940]) [#1934]: https://github.com/tokio-rs/axum/pull/1934 +[#1940]: https://github.com/tokio-rs/axum/pull/1940 # 0.6.15 (12. April, 2023) diff --git a/axum/src/docs/routing/fallback.md b/axum/src/docs/routing/fallback.md index f2b5d3331e..11b25896ef 100644 --- a/axum/src/docs/routing/fallback.md +++ b/axum/src/docs/routing/fallback.md @@ -26,3 +26,38 @@ async fn fallback(uri: Uri) -> (StatusCode, String) { Fallbacks only apply to routes that aren't matched by anything in the router. If a handler is matched by a request but returns 404 the fallback is not called. + +# Handling all requests without other routes + +Using `Router::new().fallback(...)` to accept all request regardless of path or +method, if you don't have other routes, isn't optimal: + +```rust +use axum::Router; + +async fn handler() {} + +let app = Router::new().fallback(handler); + +# async { +axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + .serve(app.into_make_service()) + .await + .unwrap(); +# }; +``` + +Running the handler directly is faster since it avoids the overhead of routing: + +```rust +use axum::handler::HandlerWithoutStateExt; + +async fn handler() {} + +# async { +axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) + .serve(handler.into_make_service()) + .await + .unwrap(); +# }; +``` From 994492d427c116a57f3ddca07ea61c0b3b8a5453 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 17 Apr 2023 13:46:54 +0200 Subject: [PATCH 14/15] Add test for not hitting `debug_assert` in `MatchedPath` (#1935) --- axum/src/extract/matched_path.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/axum/src/extract/matched_path.rs b/axum/src/extract/matched_path.rs index 02994c51e0..c4f9984e24 100644 --- a/axum/src/extract/matched_path.rs +++ b/axum/src/extract/matched_path.rs @@ -172,11 +172,14 @@ fn append_nested_matched_path(matched_path: &Arc, extensions: &http::Extens mod tests { use super::*; use crate::{ - handler::HandlerWithoutStateExt, middleware::map_request, routing::get, test_helpers::*, + body::Body, + handler::HandlerWithoutStateExt, + middleware::map_request, + routing::{any, get}, + test_helpers::*, Router, }; use http::{Request, StatusCode}; - use hyper::Body; #[crate::test] async fn extracting_on_handler() { @@ -355,6 +358,26 @@ mod tests { assert_eq!(res.status(), StatusCode::OK); } + // https://github.com/tokio-rs/axum/issues/1579 + #[crate::test] + async fn doesnt_panic_if_router_called_from_wildcard_route() { + use tower::ServiceExt; + + let app = Router::new().route( + "/*path", + any(|req: Request| { + Router::new() + .nest("/", Router::new().route("/foo", get(|| async {}))) + .oneshot(req) + }), + ); + + let client = TestClient::new(app); + + let res = client.get("/foo").send().await; + assert_eq!(res.status(), StatusCode::OK); + } + #[crate::test] async fn cant_extract_in_fallback() { async fn handler(path: Option, req: Request) { From c31bea2f96d6e4ce20b32e556b56cca0b51c41de Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 18 Apr 2023 20:40:42 +0200 Subject: [PATCH 15/15] Release axum and axum-extra (#1942) --- axum-extra/CHANGELOG.md | 4 ++++ axum-extra/Cargo.toml | 2 +- axum/CHANGELOG.md | 4 ++++ axum/Cargo.toml | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 59944f0e54..67955a67ea 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning]. # Unreleased +- None. + +# 0.7.4 (18. April, 2023) + - **added:** Add `Html` response type ([#1921]) - **added:** Add `Css` response type ([#1921]) - **added:** Add `JavaScript` response type ([#1921]) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index 847215f9fb..dca9f07d66 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" name = "axum-extra" readme = "README.md" repository = "https://github.com/tokio-rs/axum" -version = "0.7.3" +version = "0.7.4" [features] default = [] diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index b462c03434..c0f92e36d5 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- None. + +# 0.6.16 (18. April, 2023) + - **fixed:** Don't allow extracting `MatchedPath` in fallbacks ([#1934]) - **fixed:** Fix panic if `Router` with something nested at `/` was used as a fallback ([#1934]) - **added:** Document that `Router::new().fallback(...)` isn't optimal ([#1940]) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index c681ad797d..5f82592b6a 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum" -version = "0.6.15" +version = "0.6.16" categories = ["asynchronous", "network-programming", "web-programming::http-server"] description = "Web framework that focuses on ergonomics and modularity" edition = "2021"