Commit Graph
221 Commits
Author SHA1 Message Date
David PedersenandGitHub f3c155bf5b Fix typo in StreamBody docs (#343) 2021-09-22 14:20:43 +00:00
ImbolcandGitHub 4be4e1d17c Fix doc typo (#342) 2021-09-22 14:07:26 +00:00
David PedersenandGitHub 48401f2c8d Simplify macros for implementing Handler and FromRequest (#340)
Makes the generated docs nicer and avoids having a recursive macro.
2021-09-19 15:51:25 +00:00
David PedersenandGitHub 0b9e0c7508 Refactor internal testing setup (#338)
This changes the test setup to use our own client rather than using
reqwest directly. Allows us to add several quality of life features like
always unwrapping results.
2021-09-19 09:38:34 +00:00
David PedersenandGitHub 2a683417d1 Clarify what handler::any and service::any accepts (#337)
They only accept standard HTTP methods and don't think we can fix that
without breaking changes.

Fixes https://github.com/tokio-rs/axum/issues/289
2021-09-19 08:28:15 +00:00
David PedersenandGitHub 593e3e319a Improve extractor docs (#327)
* Improve extractor docs

- Moves things from the `extract` module docs to the root module docs to
  make them more discoverable
- Adds section showing commonly used extractors
- More clarity around multiple extractors that mutate the request

* english...
2021-09-18 19:09:53 +02:00
Olivier PinonandGitHub 0c18caa10f Add accessors to TypedHeaderRejection fields (#317)
Fixes #316
2021-09-12 16:11:51 +00:00
heliumbrainandGitHub 3741e16cf2 Fix typos in docs (#318)
* Fix typo in docs

Typos for FromRequest

* One more typo
2021-09-12 14:25:25 +00:00
Sidharth KshatriyaandGitHub 39c5cc24fa Remove repeated constraint (#294)
The constraint for B is repeated. Remove it.
2021-08-31 08:23:59 +00:00
David PedersenandGitHub e698586193 Document adding middleware to multiple groups of routes (#293) 2021-08-31 07:07:57 +00:00
David PedersenandGitHub c082107fb6 Document using StreamExt::split with WebSocket (#291)
Fixes https://github.com/tokio-rs/axum/issues/283
2021-08-30 21:13:50 +00:00
Ken-MiuraandGitHub c7c73a0d72 Fix a typo in documentation (#280) 2021-08-27 20:20:59 +02:00
Jonas PlatteandGitHub 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 PedersenandGitHub 4c5068c01f Add Send + Sync check to all services (#277)
Should prevent us accidentally introducing breaking changes.

Fixes https://github.com/tokio-rs/axum/issues/275
2021-08-26 20:59:55 +02:00
David PedersenandGitHub a0be328976 Revert "Remove buffer from BoxRoute (#270)" (#273)
This reverts commit 552d69e5d4.
2021-08-26 14:11:38 +00:00
David PedersenandGitHub 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 PedersenandGitHub 20f6c3b509 Remove needless traits bounds from Router::boxed (#269)
Turns out these bounds weren't actually needed.

I was hoping it would speed up compile times but that isn't the case.
2021-08-26 08:24:21 +02:00
David PedersenandGitHub 0d2db387a8 Fix URI captures matching empty segments (#264)
It was never the intention that `/:key` should match `/`. This fixes
that.

Part of https://github.com/tokio-rs/axum/issues/259
2021-08-24 18:27:06 +00:00
Jonas PlatteandGitHub ab207af060 Rename .route() / .nest() "description" to "path" (#265) 2021-08-24 19:29:28 +02:00
David PedersenandGitHub 3d4ef9dc32 Document how to implement IntoResponse for custom error type (#258)
Fixes https://github.com/tokio-rs/axum/issues/249
2021-08-24 12:42:10 +02:00
Jonas PlatteandGitHub 536b8ca4ec Add Redirect::to constructor (#255)
The motivation for this is established in the issue it fixes.

Resolves #248
2021-08-24 12:13:18 +02:00
bearandGitHub 52ccb1bf42 Update StreamBody doc link (#253) 2021-08-24 07:58:35 +02:00
David Pedersen 0ab6ea6b6a Mention tower-log feature in docs 2021-08-23 18:40:18 +02:00
David PedersenandGitHub 6777dc1e5c Add constructor to Sse (#244)
I think this is more consistent with the rest of the API
2021-08-23 17:51:30 +02:00
David PedersenandGitHub a753eac23f Remove boxing from StreamBody (#241)
I just had a thought: Why should `response::Headers` be generic, but
`body::StreamBody` should not? `StreamBody` previously boxed the stream
to erase the generics. So we had `response::Headers<T>` but
`body::StreamBody`, without generics.

After thinking about it I think it actually makes sense for responses to
remain generic because you're able to use `impl IntoResponse` so you
don't have to name the generics.

Whereas in the case of `BodyStream` (an extractor) you cannot use `impl Trait`
so it makes sense to box the inner body to make the type easier to name. Besides,
`BodyStream` is mostly useful when the request body isn't `hyper::Body`, as
that already implements `Stream`.
2021-08-22 22:03:56 +02:00
David PedersenandGitHub dbab5a84b4 Expand middleware docs (#239)
Adds docs on
- Commonly used middleware
- Writing your own middleware
- Links to tower's guides
2021-08-22 15:56:56 +02:00
David PedersenandGitHub 2322d39800 Add StreamBody (#237)
This adds `StreamBody` which converts a `Stream` of `Bytes` into a `http_body::Body`.

---

As suggested by Kestrer on Discord it would make sense for axum to provide different kinds of body types other than `Empty`, `Full`, and `hyper::Body`. There is also some talk about [splitting up `hyper::Body`](https://github.com/hyperium/hyper/issues/2345) so this can be seen as getting started on that effort. axum's body types could be moved to hyper or http-body if thats the direction we decide on.

The types I'm thinking about adding are:

- `StreamBody`-  added in this PR
- `AsyncReadBody` - similar to [http-body#41](https://github.com/hyperium/http-body/pull/41/files)
- `ChannelBody` - similar to `hyper::Body::channel`
2021-08-22 14:41:51 +02:00
David PedersenandGitHub 80a8355eff Remove generic parameter from BodyStream (#234)
I think `BodyStream` is more useful without being generic over the
request body.

I'm also looking into adding a response body from a stream called
`StreamBody` which will work pretty much opposite to this.
2021-08-22 11:33:38 +02:00
David PedersenandGitHub add3dc36f9 Rename extract::Body to extract::RawBody (#233) 2021-08-21 20:04:39 +02:00
David PedersenandGitHub fbd43c6600 Document not being able to mix fallible and infallible routes (#232)
I haven't been able to find a proper solution for #89 so for now I think
we should document the issue and move on with shipping 0.2.

Part of https://github.com/tokio-rs/axum/issues/89
2021-08-21 15:36:50 +02:00
David PedersenandGitHub 8a61b9ffe1 Use std::future::ready (#231)
`std::future::ready` has been stable since 1.48 so since axum's MSRV is
1.51 we can use this rather the one from `futures_util`.
2021-08-21 15:18:05 +02:00
David PedersenandGitHub 82dc847d47 Fix nest docs inconsistency (#230) 2021-08-21 15:06:15 +02:00
David PedersenandGitHub 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
David PedersenandGitHub 35ea7ca0ff Mention using Path<HashMap> to capture all params (#228)
Might not be entirely obvious that you can do this so makes sense to
mention in the docs.
2021-08-21 14:21:31 +02:00
David PedersenandGitHub 0d8f8b7b6c Fallback to calling next route if no methods match (#224)
This removes a small foot gun from the routing.

This means matching different HTTP methods for the same route that
aren't defined together now works.

So `Router::new().route("/", get(...)).route("/", post(...))` now
accepts both `GET` and `POST`. Previously only `POST` would be accepted.
2021-08-21 01:00:12 +02:00
David PedersenandGitHub 971c0a394a Revert "Simplify handler trait (#221)" (#223)
This reverts commit 44c58bdf5f.
2021-08-20 23:56:16 +02:00
David PedersenandGitHub f984198440 Add more examples to "Building responses" section (#222)
Someone on reddit suggested adding more examples.
2021-08-20 20:50:11 +02:00
David PedersenandGitHub 44c58bdf5f Simplify handler trait (#221)
Rely on the `impl FromRequest for (T, ...)` rather than extracting things directly inside the macro.
2021-08-20 20:36:34 +02:00
David PedersenandGitHub e8bc3f5082 Further compile time improvements (#220)
This improves compiles further when using lots of nested routes. Such as
the example posted
[here](https://github.com/tokio-rs/axum/issues/200#issuecomment-902541073).

It seems rustc is really slow at checking bounds on these kinds of
intermediate builder methods. Should probably file an issue about that.
2021-08-20 19:51:29 +02:00
David Pedersen 23dcf3631e Export Or in a more consistent way 2021-08-19 22:44:26 +02:00
David Pedersen 570e13195c Inline Router in root module docs 2021-08-19 22:39:37 +02:00
David PedersenandGitHub ca4d9a2bb9 Replace route with Router::new().route() (#215)
This way there is now only one way to create a router:

```rust
use axum::{Router, handler::get};

let app = Router::new()
    .route("/foo", get(handler))
    .route("/foo", get(handler));
```

`nest` was changed in the same way:

```rust
use axum::Router;

let app = Router::new().nest("/foo", service);
```
2021-08-19 22:37:48 +02:00
David PedersenandGitHub 97b53768ba Replace RoutingDsl trait with Router type (#214)
* Remove `RoutingDsl`

* Fix typo
2021-08-19 21:24:32 +02:00
David PedersenandGitHub 0fd17d4181 Bring back Handler::into_service (#212)
It was removed as part of https://github.com/tokio-rs/axum/pull/184 but
I do actually think it has some utility. So makes sense to keep even if
axum doesn't use it directly for routing.
2021-08-19 21:16:44 +02:00
Eduardo CanellasandGitHub 2d6b5dd0b8 docs: fix typo on EmptyRouter documentation (#204) 2021-08-18 20:30:39 +02:00
David PedersenandGitHub e22045d42f Change nested routes to see the URI with prefix stripped (#197) 2021-08-18 09:48:36 +02:00
Florian ThelliezandGitHub d9a06ef14b Remove axum::prelude (#195) 2021-08-18 00:04:15 +02:00
David PedersenandGitHub dd0c345040 Improve compile times of handle_error and check_infallible (#198)
* Improve compile times of `handle_error`

This brings the compile time of the example posted [here][example] from
3 seconds down to 0.3 seconds for me.

Having the bounds on the methods does improve UX but not worth
sacrificing 10x compile time for.

[example]: https://github.com/tokio-rs/axum/issues/145#issue-963183256

* Improve compile time of `check_infallible`

* update changelog
2021-08-17 19:07:47 +02:00
David PedersenandGitHub 93cdfe8c5f Work around for http2 hang with Or (#199)
This is a nasty hack that works around
https://github.com/hyperium/hyper/issues/2621.

Fixes https://github.com/tokio-rs/axum/issues/191
2021-08-17 19:00:24 +02:00
David PedersenandGitHub 97c140cdf7 Add Headers response (#193)
* Add `Headers`

Example usage:

```rust
use axum::{
    route,
    routing::RoutingDsl,
    response::{IntoResponse, Headers},
    handler::get,
};
use http::header::{HeaderName, HeaderValue};

// It works with any `IntoIterator<Item = (Key, Value)>` where `Key` can be
// turned into a `HeaderName` and `Value` can be turned into a `HeaderValue`
//
// Such as `Vec<(HeaderName, HeaderValue)>`
async fn just_headers() -> impl IntoResponse {
    Headers(vec![
        (HeaderName::from_static("X-Foo"), HeaderValue::from_static("foo")),
    ])
}

// Or `[(&str, &str)]`
async fn from_strings() -> impl IntoResponse {
    Headers([("X-Foo", "foo")])
}
```

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

* Make work on Rust versions without `IntoIterator` for arrays

* format

* changelog
2021-08-17 17:28:02 +02:00