Commit graph

1533 commits

Author SHA1 Message Date
Sagi Sarussi
cb637f1124
Fix typo (#202) 2021-08-18 08:02:45 +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
1ae0dee53b Update changelog 2021-08-18 00:04:57 +02:00
Florian Thelliez
d9a06ef14b
Remove axum::prelude (#195) 2021-08-18 00:04:15 +02:00
David Pedersen
f083c3e97e Ignore error in TLS example 2021-08-17 22:17:04 +02:00
David Pedersen
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 Pedersen
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 Pedersen
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
David Pedersen
baa99e5084
Make RequestParts::{new, try_into_request} public (#194)
Fixes https://github.com/tokio-rs/axum/issues/147
2021-08-16 20:55:22 +02:00
David Pedersen
b4cbd7f147
Add Redirect response (#192)
* Add `Redirect` response

* Add `Redirect::found`
2021-08-16 19:48:03 +02:00
David Pedersen
dda625759d Fix import 2021-08-16 17:29:47 +02:00
David Pedersen
a128a672a1
Remove allocation when calling handler (#190)
`Handler::call` already returns a boxed future so we don't have to box
it again.
2021-08-16 09:19:37 +02:00
Eduardo Canellas
57e440ed2e
move relevant docs sections to be under "Routing" (#175) 2021-08-16 09:17:26 +02:00
David Pedersen
be7e9e9bc6
Refactor TypedHeader extractor (#189)
I should use `HeaderMapExt::typed_try_get` rather than implementing it
manually.
2021-08-16 09:05:10 +02:00
David Pedersen
48afd30491
Improve compile times (#184)
* Inline `handler::IntoService`

* Inline `BoxResponseBody`

* Add missing debug impl
2021-08-15 23:01:26 +02:00
Harrison Burt
292d174a73
correct hyper link to add the .rs suffix. (#183)
This fixes the broken redirect for `tracing_aka_logging` which current redirects to what it thinks is a folder instead of a file, resulting in a 404.
2021-08-15 22:55:33 +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
594052dc48 Revert hello_world example
Accidentally changed it while testing something
2021-08-10 10:03:29 +02:00
David Pedersen
8ef96f2199 Add test for routing matching multiple methods
I don't believe we had a test for this
2021-08-10 10:02:40 +02:00
Richard Janis Goldschmidt
387de8b426
Use tower http auth layer in kv store example (#171) 2021-08-10 09:46:09 +02:00
David Pedersen
a790abca87 More consistent docs 2021-08-08 20:26:42 +02:00
David Pedersen
8500ea256d
Implement FromRequest for http::Extensions (#169)
Not sure its very useful but odd to not provide this. All other request
parts have an extractor and we already have the rejection for it.
2021-08-08 20:01:06 +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
David Pedersen
bc27b09f5c
Make sure nested services still see full URI (#166)
They'd previously see the nested URI as we mutated the request. Now we
always route based on the nested URI (if present) without mutating the
request. Also meant we could get rid of `OriginalUri` which is nice.
2021-08-08 17:27:23 +02:00
fluunke
0674c9136a
Add oauth2 example (#144) 2021-08-08 17:22:24 +02:00
David Pedersen
d89d061724
Move some files to mod.rs (#165)
I prefer this setup but didn't wanna do it while there were too many
open PRs.
2021-08-08 16:54:03 +02:00
David Pedersen
b75b7b0184
Re-export more body utilities (#162)
Useful when implementing `IntoResponse`
2021-08-08 16:41:17 +02:00
David Pedersen
b4bdddf9d2
Add NestedUri (#161)
Fixes https://github.com/tokio-rs/axum/issues/159
2021-08-08 14:45:31 +02:00
David Pedersen
8013165908
Move methods from ServiceExt to RoutingDsl (#160)
Previously, on `main`, this wouldn't compile:

```rust
let app = route("/", get(handler))
    .layer(
        ServiceBuilder::new()
            .timeout(Duration::from_secs(10))
            .into_inner(),
    )
    .handle_error(...)
    .route(...); // <-- doesn't work
```

That is because `handle_error` would be
`axum::service::ServiceExt::handle_error` which returns `HandleError<_,
_, _, HandleErrorFromService>` which does _not_ implement `RoutingDsl`.
So you couldn't call `route`. This was caused by
https://github.com/tokio-rs/axum/pull/120.

Basically `handle_error` when called on a `RoutingDsl`, the resulting
service should also implement `RoutingDsl`, but if called on another
random service it should _not_ implement `RoutingDsl`.

I don't think thats possible by having `handle_error` on `ServiceExt`
which is implemented for any service, since all axum routers are also
services by design.

This resolves the issue by removing `ServiceExt` and moving its methods
to `RoutingDsl`. Then we have more tight control over what has a
`handle_error` method.

`service::OnMethod` now also has a `handle_error` so you can still
handle errors from random services, by doing
`service::any(svc).handle_error(...)`.
2021-08-08 14:30:51 +02:00
David Pedersen
9b3f3c9bdf Fix docs typo 2021-08-07 23:37:07 +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
David Pedersen
36c8d97059 Reorder changelog a bit 2021-08-07 22:35:41 +02:00
David Pedersen
6ce355cca3
Add unique future types for all services (#157)
So we can more easily change them in the future.
2021-08-07 22:27:27 +02:00
David Pedersen
2389761ce7
Add dedicated tracing/logging example (#155)
Useful to link to since several have asked.
2021-08-07 22:11:55 +02:00
David Pedersen
85bb0158be Fix outdated docs 2021-08-07 22:11:27 +02:00
David Pedersen
c570fb2d52
Fix Uri extractor not being the full URI if using nest (#156) 2021-08-07 22:07:50 +02:00
David Pedersen
a6b3e09827
Remove UrlParamsMap and UrlParams (#154)
Use `extract::Path` instead.
2021-08-07 21:22:08 +02:00
David Pedersen
6a82dd75ea
Implement std::error::Error for all rejections (#153) 2021-08-07 21:03:04 +02:00
David Pedersen
a38d5c3592
Re-export http_body::Body (#152)
Makes writing `IntoResponse` impls easier
2021-08-07 20:29:24 +02:00
David Pedersen
4bb17cbc2d
Remove take_* methods from RequestParts for Version, Method, and Uri (#151)
* Remove `RequestParts::take_method`

* Remove `RequestParts::take_uri`

* Remove `RequestParts::take_version`
2021-08-07 20:24:13 +02:00
David Pedersen
75b5615ccd
Add axum::Error (#150)
Replace `BoxStdError` and supports downcasting
2021-08-07 19:56:44 +02:00
Grzegorz Baranski
4792d0c15c
Make ws::Message an enum for easier frame type matching (#116)
* feat(ws): make Message an enum to allow pattern matching

* fix(examples): update to new websockets `Message`

* fix(ws): remove wildcard imports

* fix(examples/chat): apply clippy's never_loop

* style: `cargo fmt`

* docs:add license notes above parts that are copied

* fix(ws): make CloseCode an alias to u16

* fix: move Message from src/ws/mod.rs to src/extract/ws.rs

* docs: add changelog entry about websocket messages

* fix: remove useless convertions to the same type
2021-08-07 19:47:22 +02:00
David Pedersen
ab927033b3
Support returning any http_body::Body from IntoResponse (#86)
Adds associated `Body` and `BodyError` types to `IntoResponse`. This is required for returning responses with bodies other than `hyper::Body` from handlers. That wasn't previously possible.

This is a breaking change so should be shipped in 0.2.
2021-08-07 18:03:21 +02:00
David Pedersen
4194cf70da
Change WebSocket API to use an extractor (#121)
Fixes https://github.com/tokio-rs/axum/issues/111

Example usage:

```rust
use axum::{
    prelude::*,
    extract::ws::{WebSocketUpgrade, WebSocket},
    response::IntoResponse,
};

let app = route("/ws", get(handler));

async fn handler(ws: WebSocketUpgrade) -> impl IntoResponse {
    ws.on_upgrade(handle_socket)
}

async fn handle_socket(mut socket: WebSocket) {
    while let Some(msg) = socket.recv().await {
        let msg = if let Ok(msg) = msg {
            msg
        } else {
            // client disconnected
            return;
        };

        if socket.send(msg).await.is_err() {
            // client disconnected
            return;
        }
    }
}
```
2021-08-07 17:26:23 +02:00
David Pedersen
404a3b5e8a
Add default for RequestParts type param (#148)
Same reason as https://github.com/tokio-rs/axum/pull/146
2021-08-07 17:10:00 +02:00
David Pedersen
045ec57d92
Add RouteDsl::or to combine routes (#108)
With this you'll be able to do:

```rust
let one = route("/foo", get(|| async { "foo" }))
    .route("/bar", get(|| async { "bar" }));

let two = route("/baz", get(|| async { "baz" }));

let app = one.or(two);
```

Fixes https://github.com/tokio-rs/axum/issues/101
2021-08-07 17:09:45 +02:00
David Pedersen
b1e7a6ae7b
Enable CI caching (#149) 2021-08-07 17:09:30 +02:00
David Pedersen
95d7582d28
Fix ServiceExt::handle_error footgun (#120)
As described in
https://github.com/tokio-rs/axum/pull/108#issuecomment-892811637, a
`HandleError` created from `axum::ServiceExt::handle_error` should _not_
implement `RoutingDsl` as that leads to confusing routing behavior.

The technique used here of adding another type parameter to
`HandleError` isn't very clean, I think. But the alternative is
duplicating `HandleError` and having two versions, which I think is less
desirable.
2021-08-07 16:44:12 +02:00