Commit Graph

97 Commits

Author SHA1 Message Date
19年梦醒 fc3cb44b91
Upgrade headers to 0.4.0 (#2351) 2023-11-25 08:47:57 +00:00
David Pedersen 43b14a5f02
Update to latest versions of hyper and http-body (#1882)
Co-authored-by: Michael Scofield <mscofield0@tutanota.com>
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
2023-11-23 11:03:03 +00:00
Jonas Platte dc480f7b0e
Re-integrate examples into the root workspace (#1466)
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2023-03-10 12:02:11 +01:00
Jonas Platte f0f60681ef
Fix intra-doc links on docs.rs (#1205)
* Use version 2 of Cargo's feature resolver

* Increase minimum version of tungstenite

Tungstenite 0.17.1 has a higher MSRV, and there should be no reason to
use it over 0.17.2.

* Clean up and fix MSRV CI job

* Fix some intra-doc links not resolving correctly on docs.rs

* Bump minimum version of tower

… to avoid dependencies with a broken minimum-versions chain.
2022-07-28 19:14:31 +02:00
David Pedersen f6b1d35c51
Use minimal dependency versions for MSRV tests on CI (#1109) 2022-06-27 20:44:20 +02:00
David Pedersen 1fe4558362
Move examples to separate workspace (#978)
* Move examples to separate workspace

* update commands to run examples

* remove debug
2022-04-29 18:53:41 +02:00
David Pedersen 19eda2f566 fix ups: include all crates in workspace 2022-03-08 21:32:54 +01:00
David Pedersen dfa2e3b09f
Fix nesting of opaque services that paths that contain params (#841)
* checkpoint

* fix it

* more consistent macros

* fix msrv
2022-03-08 21:27:44 +01:00
David Pedersen 15914c4f15
Remove axum-debug code (#740) 2022-01-31 22:46:44 +01:00
David Pedersen e4c389c94d
Add `axum-macros` crate with `#[derive(FromRequest)]` (#718)
* initial working impl

* support `#[from_request(via(...))]`

* support extracting the whole thing at once

* rely on type inference

* fix footgun

* fix typo

* generate rejection enums

* move tests to trybuild

* minor clean up

* docs

* Support multiple generic extractors with same "via" type

* support `Result` as well

* Update axum-macros/src/from_request.rs

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>

* Add `#[automatically_derived]`

* remove needless `#[derive(Debug)]` on macro types

* Fix error messages that different for some reason

* Update axum-macros/src/lib.rs

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>

* add more `#[automatically_derived]`

* support same types in tuple structs

* update docs

* prep axum-macros for release

* address review feedback

* Update axum-macros/src/lib.rs

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>

* Update axum-macros/src/lib.rs

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>

* Update known limitation

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
2022-01-25 15:05:50 +01:00
David Pedersen 254d8fde17
Move `FromRequest` and `IntoResponse` into new `axum-core` crate (#564)
* Move `IntoResponse` to axum-core

* Move `FromRequest` to axum-core

* some clean up

* Remove hyper dependency from axum-core

* Fix docs reference

* Use default

* Update changelog

* Remove mention of default type
2021-11-30 13:46:13 +00:00
David Pedersen a16ea65031
Add `axum-extra` crate (#536)
* Add `axum-extra` crate

Empty for now but now we have a place to put stuff.

I'll make a PR for moving over https://github.com/davidpdrsn/axum-resource.

* remove `authors` field from `Cargo.toml`s
2021-11-17 20:00:32 +00:00
David Pedersen 9a410371a6
Move `axum-handle-error-extract` into axum (#534)
* Move `axum-handle-error-extract` into axum

With 0.4 underway we can now nuke `axum-handle-error-extract` and move
its code directly into axum.

So this replaces the old `HandleErrorLayer` with one that supports async
functions and extractors.

* changelog

* fix CI
2021-11-17 19:09:58 +00:00
David Pedersen 5da3f34b3e
Clean up axum-debug crate (#498)
* Clean up axum-debug crate

Mostly just brings the crate more in line with the rest of the crates in
the workspace.

I've also removed the axum-debug-macros crate since axum-debug only
contained one re-export from axum-debug-macros. So we didn't need two
crates. Can always bring the "backend" crate back if we need things in
axum-debug that aren't proc-macros.

* Just testing: This should make CI fail

* Misc CI clean up

* fix intentional breakage

* fix changelog

* Remove rustfmt config for now as it gives warnings on stable

* Fix paths
2021-11-11 17:33:33 +01:00
Eray Karatay f6b47478da
Move `axum-debug` crate to workspace (#497)
* add axum-debug to workspace

* update readme

* add changes to changelog

* little docs update

* fix the gap

a tab has leaked into workspace Cargo.toml, it must be fixed

* address clippy warnings
2021-11-11 15:18:40 +01:00
David Pedersen 7e40312cff
Add `axum-handle-error-extract` crate (#468)
* Add `axum-handle-error-extract` crate

* fixup

* Fix readme link

* Add `HandleErrorExt` for new error handling middleware

* Revert "fixup"

This reverts commit 83e04954df.
2021-11-08 19:24:54 +01:00
David Pedersen ba4d8a2357
Move axum crate into workspace subfolder (#458)
* Move axum crate into workspace subfolder

Over time I imagine we're gonna have other crates in this repo that
provide utilities or integrations for axum. This prepares for that by
moving the main axum crate into its own folder.

The README situation is a bit annoying because we want `./README.md`
for viewing the repo on github but `axum/README.md` for crates.io. For
now I've just copy/pasted it and added CI step to make sure they're
identical.

* update changelog link

* Add licenses to all examples

* is this how you install `diff`?

* or maybe this is how?

* fix readme links

* like this?

* fix cargo-deny step

* Try making root readme a symlink

* remove compare readme step

not needed since readme in repo root is now a symlink

* Revert "Add licenses to all examples"

This reverts commit ab321b7fb9.
2021-11-03 12:38:48 +01:00
David Pedersen 3d07d40e02
Remove `Sync` requirement from response bodies (#440)
As we learned [in Tonic] bodies don't need to be `Sync` because they can
only be polled from one thread at a time.

This changes axum's bodies to no longer require `Sync` and makes
`BoxBody` an alias for `UnsyncBoxBody<Bytes, axum::Error>`.

[in Tonic]: https://github.com/hyperium/tonic/issues/117
2021-11-01 22:37:16 +01:00
David Pedersen 9465128f3e
Rework docs (#437)
This reworks axum's docs in an attempt to make things easier to find. Previously I wasn't a fan of those docs for the same topic were spread across the root module docs and more specific places like types and methods.

This changes it such that the root module docs only gives a high level introduction to a topic, perhaps with a small example, and then link to other places where all the details are. This means `Router` is now the single place to learn about routing, and etc for the topics like handlers and error handling.
2021-11-01 21:13:37 +00:00
David Pedersen 1c6038c09f
Depend on tower 0.4.10 (#439)
axum uses `impl Layer for ServiceBuilder` internally which requires
tower 0.4.10. Making that explicit should help users updating to axum
0.3 without also having to manually bump their tower dependency.
2021-11-01 13:15:34 +00:00
David Pedersen 9b17d86b92
Update to matchit 0.4.4 (#417)
- Static vs dynamic paths are now supported meaning `/foo` and `/:key`
  are not considered to overlap.
- A bug we hit regarding trailing slashes is fixed.
2021-10-25 23:35:33 +00:00
David Pedersen 02a035fb14
Add `MatchedPath` extractor (#412)
Fixes #386
2021-10-25 21:38:29 +00:00
David Pedersen 0ee7379d4f
Fix compile time regression by boxing routes internally (#404)
This is a reimplementation of #401 but with the new matchit based router.

Fixes #399
2021-10-24 20:52:42 +02:00
David Pedersen 1a78a3f224
"matchit" based router (#363)
* "matchit" based router

* Update changelog

* Remove dependency on `regex`

* Docs

* Fix typos

* Also mention route order in root module docs

* Update CHANGELOG.md

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>

* Document that `/:key` and `/foo` overlaps

* Provide good error message for wildcards in routes

* minor clean ups

* Make `Router` cheaper to clone

* Ensure middleware still only applies to routes above

* Remove call to issues from changelog

We're aware of the short coming :)

* Fix tests on 1.51

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
2021-10-24 15:22:49 +02:00
David Pedersen e71ab44bd5 Update main to version 0.3.0 2021-10-19 22:37:57 +02:00
David Pedersen ce5834ab80
Expand accepted content types for JSON requests (#378)
* Expand accepted content types for JSON requests

Fixes https://github.com/tokio-rs/axum/issues/375

* changelog

* add test for content type without spaces

* Don't accept `text/json`

* small clean up
2021-10-08 14:51:22 +00:00
Jonas Platte d29c1f26c2
Bump minimum version of async-trait (#369)
Older versions generate invalid code for Handler impls.
2021-10-06 20:46:10 +02:00
David Pedersen afabded385
Percent decode automatically in `extract::Path` (#272)
* Percent decode automatically in `extract::Path`

Fixes https://github.com/tokio-rs/axum/issues/261

* return an error if path param contains invalid utf-8

* Mention automatic decoding in the docs

* Update changelog: This is a breaking change

* cleanup

* fix tests
2021-10-02 14:04:29 +00:00
Jonas Platte 250ea0cfef
Make JSON and HTTP1 support optional (#286)
* Make json support an optional feature

* Fix Json type documentation

* Make hyper's http1 feature optional
2021-10-02 15:46:33 +02:00
David Pedersen 7fae35020a
Version 0.2.6 (#360) 2021-10-02 11:33:52 +00:00
David Pedersen bb5bcab116
Version 0.2.5 (#336)
- Add accessors for `TypedHeaderRejection` fields ([#317])
- Improve docs for extractors ([#327])

[#317]: https://github.com/tokio-rs/axum/pull/317
[#327]: https://github.com/tokio-rs/axum/pull/327
2021-09-18 19:35:10 +02:00
David Pedersen 36665793c3
Version 0.2.4 (#313)
Contains some documentation improvements:

- Document using `StreamExt::split` with `WebSocket` ([#291])
- Document adding middleware to multiple groups of routes ([#293])

[#291]: https://github.com/tokio-rs/axum/pull/291
[#293]: https://github.com/tokio-rs/axum/pull/293
2021-09-10 16:01:14 +00:00
Jonas Platte e41bac7f39
Expose hyper's http2 feature flag (#279)
* Order features alphabetically

… in Cargo.toml and crate docs.

* Improve ws feature docs

* Expose hyper's http2 feature flag
2021-08-27 08:58:50 +00:00
David Pedersen bdc39deef2
Version 0.2.3 (#274)
0.2.3 (26. August, 2021)

- **fixed:** Fix accidental breaking change introduced by internal refactor.
  `BoxRoute` used to be `Sync` but was accidental made `!Sync` ([#273](https://github.com/tokio-rs/axum/pull/273))
2021-08-26 16:22:47 +02:00
David Pedersen a0be328976
Revert "Remove buffer from `BoxRoute` (#270)" (#273)
This reverts commit 552d69e5d4.
2021-08-26 14:11:38 +00:00
David Pedersen 7b391d85c8
Version 0.2.2 (#271)
0.2.2 (24. August, 2021)

- **fixed:** Fix URI captures matching empty segments. This means requests with
  URI `/` will no longer be matched by `/:key` ([#264](https://github.com/tokio-rs/axum/pull/264))
- **fixed:** Remove needless trait bounds from `Router::boxed` ([#264](https://github.com/tokio-rs/axum/pull/264))
2021-08-26 06:57:07 +00:00
David Pedersen 552d69e5d4
Remove buffer from `BoxRoute` (#270)
Boxing a service normally means using `tower::util::BoxService`. That
doesn't implement `Clone` however so normally I had been combining it
with `Buffer` to get that.

But recently I discovered https://github.com/dtolnay/dyn-clone which
makes it possible to clone trait objects. So this adds a new internal
utility called `CloneBoxService` which replaces the previous
`BoxService` + `Buffer` combo in `BoxRoute`.

I'll investigate upstreaming that to tower. I think it makes sense there
since box + clone is quite a common need.
2021-08-26 06:34:53 +00:00
David Pedersen 9a082a74b0
Version 0.2.1 (#256)
0.2.1 (24. August, 2021)

- **added:** Add `Redirect::to` constructor ([#255](https://github.com/tokio-rs/axum/pull/255))
- **added:** Document how to implement `IntoResponse` for custom error type ([#258](https://github.com/tokio-rs/axum/pull/258))
2021-08-24 12:53:57 +02:00
David Pedersen 02e61dfdd6
Version 0.2.0 (#247)
- Overall:
  - **fixed:** Overall compile time improvements. If you're having issues with compile time
    please file an issue! ([#184](https://github.com/tokio-rs/axum/pull/184)) ([#198](https://github.com/tokio-rs/axum/pull/198)) ([#220](https://github.com/tokio-rs/axum/pull/220))
  - **changed:** Remove `prelude`. Explicit imports are now required ([#195](https://github.com/tokio-rs/axum/pull/195))
- Routing:
  - **added:** Add dedicated `Router` to replace the `RoutingDsl` trait ([#214](https://github.com/tokio-rs/axum/pull/214))
  - **added:** Add `Router::or` for combining routes ([#108](https://github.com/tokio-rs/axum/pull/108))
  - **fixed:** Support matching different HTTP methods for the same route that aren't defined
    together. So `Router::new().route("/", get(...)).route("/", post(...))` now
    accepts both `GET` and `POST`. Previously only `POST` would be accepted ([#224](https://github.com/tokio-rs/axum/pull/224))
  - **fixed:** `get` routes will now also be called for `HEAD` requests but will always have
    the response body removed ([#129](https://github.com/tokio-rs/axum/pull/129))
  - **changed:** Replace `axum::route(...)` with `axum::Router::new().route(...)`. This means
    there is now only one way to create a new router. Same goes for
    `axum::routing::nest`. ([#215](https://github.com/tokio-rs/axum/pull/215))
  - **changed:** Implement `routing::MethodFilter` via [`bitflags`](https://crates.io/crates/bitflags) ([#158](https://github.com/tokio-rs/axum/pull/158))
  - **changed:** Move `handle_error` from `ServiceExt` to `service::OnMethod` ([#160](https://github.com/tokio-rs/axum/pull/160))

  With these changes this app using 0.1:

  ```rust
  use axum::{extract::Extension, prelude::*, routing::BoxRoute, AddExtensionLayer};

  let app = route("/", get(|| async { "hi" }))
      .nest("/api", api_routes())
      .layer(AddExtensionLayer::new(state));

  fn api_routes() -> BoxRoute<Body> {
      route(
          "/users",
          post(|Extension(state): Extension<State>| async { "hi from nested" }),
      )
      .boxed()
  }
  ```

  Becomes this in 0.2:

  ```rust
  use axum::{
      extract::Extension,
      handler::{get, post},
      routing::BoxRoute,
      Router,
  };

  let app = Router::new()
      .route("/", get(|| async { "hi" }))
      .nest("/api", api_routes());

  fn api_routes() -> Router<BoxRoute> {
      Router::new()
          .route(
              "/users",
              post(|Extension(state): Extension<State>| async { "hi from nested" }),
          )
          .boxed()
  }
  ```
- Extractors:
  - **added:** Make `FromRequest` default to being generic over `body::Body` ([#146](https://github.com/tokio-rs/axum/pull/146))
  - **added:** Implement `std::error::Error` for all rejections ([#153](https://github.com/tokio-rs/axum/pull/153))
  - **added:** Add `OriginalUri` for extracting original request URI in nested services ([#197](https://github.com/tokio-rs/axum/pull/197))
  - **added:** Implement `FromRequest` for `http::Extensions` ([#169](https://github.com/tokio-rs/axum/pull/169))
  - **added:** Make `RequestParts::{new, try_into_request}` public so extractors can be used outside axum ([#194](https://github.com/tokio-rs/axum/pull/194))
  - **added:** Implement `FromRequest` for `axum::body::Body` ([#241](https://github.com/tokio-rs/axum/pull/241))
  - **changed:** Removed `extract::UrlParams` and `extract::UrlParamsMap`. Use `extract::Path` instead ([#154](https://github.com/tokio-rs/axum/pull/154))
  - **changed:** `extractor_middleware` now requires `RequestBody: Default` ([#167](https://github.com/tokio-rs/axum/pull/167))
  - **changed:** Convert `RequestAlreadyExtracted` to an enum with each possible error variant ([#167](https://github.com/tokio-rs/axum/pull/167))
  - **changed:** `extract::BodyStream` is no longer generic over the request body ([#234](https://github.com/tokio-rs/axum/pull/234))
  - **changed:** `extract::Body` has been renamed to `extract::RawBody` to avoid conflicting with `body::Body` ([#233](https://github.com/tokio-rs/axum/pull/233))
  - **changed:** `RequestParts` changes ([#153](https://github.com/tokio-rs/axum/pull/153))
      - `method` new returns an `&http::Method`
      - `method_mut` new returns an `&mut http::Method`
      - `take_method` has been removed
      - `uri` new returns an `&http::Uri`
      - `uri_mut` new returns an `&mut http::Uri`
      - `take_uri` has been removed
  - **changed:** Remove several rejection types that were no longer used ([#153](https://github.com/tokio-rs/axum/pull/153)) ([#154](https://github.com/tokio-rs/axum/pull/154))
- Responses:
  - **added:** Add `Headers` for easily customizing headers on a response ([#193](https://github.com/tokio-rs/axum/pull/193))
  - **added:** Add `Redirect` response ([#192](https://github.com/tokio-rs/axum/pull/192))
  - **added:** Add `body::StreamBody` for easily responding with a stream of byte chunks ([#237](https://github.com/tokio-rs/axum/pull/237))
  - **changed:** Add associated `Body` and `BodyError` types to `IntoResponse`. This is
    required for returning responses with bodies other than `hyper::Body` from
    handlers. See the docs for advice on how to implement `IntoResponse` ([#86](https://github.com/tokio-rs/axum/pull/86))
  - **changed:** `tower::util::Either` no longer implements `IntoResponse` ([#229](https://github.com/tokio-rs/axum/pull/229))

  This `IntoResponse` from 0.1:
  ```rust
  use axum::{http::Response, prelude::*, response::IntoResponse};

  struct MyResponse;

  impl IntoResponse for MyResponse {
      fn into_response(self) -> Response<Body> {
          Response::new(Body::empty())
      }
  }
  ```

  Becomes this in 0.2:
  ```rust
  use axum::{body::Body, http::Response, response::IntoResponse};

  struct MyResponse;

  impl IntoResponse for MyResponse {
      type Body = Body;
      type BodyError = <Self::Body as axum::body::HttpBody>::Error;

      fn into_response(self) -> Response<Self::Body> {
          Response::new(Body::empty())
      }
  }
  ```
- SSE:
  - **added:** Add `response::sse::Sse`. This implements SSE using a response rather than a service ([#98](https://github.com/tokio-rs/axum/pull/98))
  - **changed:** Remove `axum::sse`. Its been replaced by `axum::response::sse` ([#98](https://github.com/tokio-rs/axum/pull/98))

  Handler using SSE in 0.1:
  ```rust
  use axum::{
      prelude::*,
      sse::{sse, Event},
  };
  use std::convert::Infallible;

  let app = route(
      "/",
      sse(|| async {
          let stream = futures::stream::iter(vec![Ok::<_, Infallible>(
              Event::default().data("hi there!"),
          )]);
          Ok::<_, Infallible>(stream)
      }),
  );
  ```

  Becomes this in 0.2:

  ```rust
  use axum::{
      handler::get,
      response::sse::{Event, Sse},
      Router,
  };
  use std::convert::Infallible;

  let app = Router::new().route(
      "/",
      get(|| async {
          let stream = futures::stream::iter(vec![Ok::<_, Infallible>(
              Event::default().data("hi there!"),
          )]);
          Sse::new(stream)
      }),
  );
  ```
- WebSockets:
  - **changed:** Change WebSocket API to use an extractor plus a response ([#121](https://github.com/tokio-rs/axum/pull/121))
  - **changed:** Make WebSocket `Message` an enum ([#116](https://github.com/tokio-rs/axum/pull/116))
  - **changed:** `WebSocket` now uses `Error` as its error type ([#150](https://github.com/tokio-rs/axum/pull/150))

  Handler using WebSockets in 0.1:

  ```rust
  use axum::{
      prelude::*,
      ws::{ws, WebSocket},
  };

  let app = route(
      "/",
      ws(|socket: WebSocket| async move {
          // do stuff with socket
      }),
  );
  ```

  Becomes this in 0.2:

  ```rust
  use axum::{
      extract::ws::{WebSocket, WebSocketUpgrade},
      handler::get,
      Router,
  };

  let app = Router::new().route(
      "/",
      get(|ws: WebSocketUpgrade| async move {
          ws.on_upgrade(|socket: WebSocket| async move {
              // do stuff with socket
          })
      }),
  );
  ```
- Misc
  - **added:** Add default feature `tower-log` which exposes `tower`'s `log` feature. ([#218](https://github.com/tokio-rs/axum/pull/218))
  - **changed:** Replace `body::BoxStdError` with `axum::Error`, which supports downcasting ([#150](https://github.com/tokio-rs/axum/pull/150))
  - **changed:** `EmptyRouter` now requires the response body to implement `Send + Sync + 'static'` ([#108](https://github.com/tokio-rs/axum/pull/108))
  - **changed:** `Router::check_infallible` now returns a `CheckInfallible` service. This
    is to improve compile times ([#198](https://github.com/tokio-rs/axum/pull/198))
  - **changed:** `Router::into_make_service` now returns `routing::IntoMakeService` rather than
    `tower::make::Shared` ([#229](https://github.com/tokio-rs/axum/pull/229))
  - **changed:** All usage of `tower::BoxError` has been replaced with `axum::BoxError` ([#229](https://github.com/tokio-rs/axum/pull/229))
  - **changed:** Several response future types have been moved into dedicated
    `future` modules ([#133](https://github.com/tokio-rs/axum/pull/133))
  - **changed:** `EmptyRouter`, `ExtractorMiddleware`, `ExtractorMiddlewareLayer`,
    and `QueryStringMissing` no longer implement `Copy` ([#132](https://github.com/tokio-rs/axum/pull/132))
  - **changed:** `service::OnMethod`, `handler::OnMethod`, and `routing::Nested` have new response future types ([#157](https://github.com/tokio-rs/axum/pull/157))
2021-08-23 20:49:29 +02:00
David Pedersen f8a0d81d79
Remove tower from axum's public API (#229)
Instead rely on `tower-service` and `tower-layer`. `tower` itself is
only used internally.

Fixes https://github.com/tokio-rs/axum/issues/186
2021-08-21 15:01:30 +02:00
Johannes Becker 7350b817b0
expose tower log feature (#218) 2021-08-20 13:41:28 +02:00
simonborje 421faceae3
Update tokio-tungstenite version (#211)
Co-authored-by: Simon Börjesson <no@address.com>
2021-08-19 14:51:21 +02:00
David Pedersen e22045d42f
Change nested routes to see the URI with prefix stripped (#197) 2021-08-18 09:48:36 +02:00
David Pedersen 6c9651c14a
Move all examples to their own crates (#201)
This makes it much clearer which dependencies each example has.
2021-08-18 00:49:01 +02:00
David Pedersen 995ffc1aa2
Correctly handle HEAD requests (#129) 2021-08-15 20:27:13 +02:00
Kai Jewson 9cd543401f
Implement SSE using responses (#98) 2021-08-14 17:29:09 +02:00
Paolo Barbolini 045287aef9
Reduce futures-util features (#173) 2021-08-10 20:08:44 +02:00
David Pedersen 6b218c7150
Clean up `RequestParts` API (#167)
In http-body 0.4.3 `BoxBody` implements `Default`. This allows us to
clean up the API of `RequestParts` quite a bit.
2021-08-08 19:48:30 +02:00
fluunke 0674c9136a
Add oauth2 example (#144) 2021-08-08 17:22:24 +02:00
David Pedersen 72071cf5de
Implement `MethodFilter` via bitflags (#158)
Fixes https://github.com/tokio-rs/axum/issues/107
2021-08-07 23:05:53 +02:00