Commit Graph

116 Commits

Author SHA1 Message Date
David Pedersen cb6f5b95b3 Minor reordering of changelog 2021-08-23 18:40:07 +02:00
David Pedersen c8c8e6509b
Add upgrade examples to the changelog (#245)
Shows how to upgrade

- General routing
- SSE
- WebSockets
2021-08-23 18:24:08 +02:00
David Pedersen 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 Pedersen b75c34b821 Fix typo 2021-08-22 16:12:05 +02:00
David Pedersen 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 Pedersen 5ae94b6a24
Group items in changelog differently (#238) 2021-08-22 14:38:59 +02:00
David Pedersen 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 Pedersen add3dc36f9
Rename `extract::Body` to `extract::RawBody` (#233) 2021-08-21 20:04:39 +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
David Pedersen 08544f9d92 Reorganize changelog a bit 2021-08-21 11:35:49 +02:00
David Pedersen 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
Johannes Becker be61b8c611
Update changelog (#219) 2021-08-20 17:23:44 +02:00
David Pedersen 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 Pedersen 97b53768ba
Replace `RoutingDsl` trait with `Router` type (#214)
* Remove `RoutingDsl`

* Fix typo
2021-08-19 21:24:32 +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 1ae0dee53b Update changelog 2021-08-18 00:04:57 +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 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 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
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 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 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 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 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 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 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
David Pedersen b5b9db47db
Remove `QueryStringMissing` as it was no longer being used (#125)
* Remove `QueryStringMissing` as it was no longer being used

* remove it in a few more places
2021-08-07 16:31:51 +02:00
David Pedersen 123b1b3c5e
Remove future re-exports (#133)
These types were moved around in
https://github.com/tokio-rs/axum/pull/130 but re-export from their old
location for backwards compatibility.

This removes the re-exports.
2021-08-07 16:22:53 +02:00
David Pedersen a0a19c8362
Remove `Copy` some impls (#132)
https://github.com/tokio-rs/axum/pull/129 was a breaking change, in part
because I had to remove the `Copy` impl from `OnMethod`.

For the sake of future proofing I think we should remove other `Copy`
impls from services as well. We can always bring them back once things
have matured more.

These types no longer implement `Copy`:

- `EmptyRouter`
- `ExtractorMiddleware`
- `ExtractorMiddlewareLayer`
2021-08-07 16:13:56 +02:00
David Pedersen 3d45a97db9
Make `FromRequest` default to use `axum::body::Body` (#146)
Most users will implement `FromRequest<axum::body::Body>` so making that
the default makes things a bit easier to use.
2021-08-07 12:22:14 +02:00
David Pedersen e13f1da11d
Version 0.1.3 (#139)
- Fix stripping prefix when nesting services at `/` ([#91](https://github.com/tokio-rs/axum/pull/91))
- Add support for WebSocket protocol negotiation ([#83](https://github.com/tokio-rs/axum/pull/83))
- Use `pin-project-lite` instead of `pin-project` ([#95](https://github.com/tokio-rs/axum/pull/95))
- Re-export `http` crate and `hyper::Server` ([#110](https://github.com/tokio-rs/axum/pull/110))
- Fix `Query` and `Form` extractors giving bad request error when query string is empty. ([#117](https://github.com/tokio-rs/axum/pull/117))
- Add `Path` extractor. ([#124](https://github.com/tokio-rs/axum/pull/124))
- Fixed the implementation of `IntoResponse` of `(HeaderMap, T)` and `(StatusCode, HeaderMap, T)` would ignore headers from `T` ([#137](https://github.com/tokio-rs/axum/pull/137))
- Deprecate `extract::UrlParams` and `extract::UrlParamsMap`. Use `extract::Path` instead ([#138](https://github.com/tokio-rs/axum/pull/138))
2021-08-06 11:20:42 +02:00
David Pedersen 811b1d896c
Deprecate `extract::UrlParams` and `extract::UrlParamsMap` (#138)
Use `extract::Path` instead. It supports everything the two other do,
and more.
2021-08-06 10:38:38 +02:00
Sunli a0ac8a5b78
Fixed the implementation of `IntoResponse` of `(HeaderMap, T)` and `(StatusCode, HeaderMap, T)` would ignore headers from `T` (#137)
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-08-06 10:31:38 +02:00
Sunli 9fdbd42fba
Implement path extractor (#124)
Fixes #42
2021-08-06 10:17:57 +02:00
Sunli fb0b3b78eb
Fix `Query` and `Form` extractors giving bad request error when query string is empty (#117)
Co-Authored-By: David Pedersen <david.pdrsn@gmail.com>

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-08-04 17:13:09 +02:00
Sunli 7cf8dafdce
Re-export http crate and hyper::Server (#110)
Co-Authored-By: David Pedersen <david.pdrsn@gmail.com>

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-08-04 12:29:42 +02:00
Sunli be68227d73
Use `pin-project-lite` instead of `pin-project` (#95) 2021-08-03 09:33:00 +02:00
Sunli ba74787532
Add support for WebSocket protocol negotiation. (#83) 2021-08-03 08:43:37 +02:00
David Pedersen 6a078ddb71
Fix stripping prefix when nesting at `/` (#91)
* Fix stripping prefix when nesting at `/`

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

* changelog
2021-08-02 22:40:33 +02:00
David Pedersen 55c1a29420
Version 0.1.2 (#80) 2021-08-01 22:13:43 +02:00
David Pedersen 6d787665d6
Server-Sent Events (#75)
Example usage:

```rust
use axum::{prelude::*, sse::{sse, Event, KeepAlive}};
use tokio_stream::StreamExt as _;
use futures::stream::{self, Stream};
use std::{
    time::Duration,
    convert::Infallible,
};

let app = route("/sse", sse(make_stream).keep_alive(KeepAlive::default()));

async fn make_stream(
    // you can also put extractors here
) -> Result<impl Stream<Item = Result<Event, Infallible>>, Infallible> {
    // A `Stream` that repeats an event every second
    let stream = stream::repeat_with(|| Event::default().data("hi!"))
        .map(Ok)
        .throttle(Duration::from_secs(1));

    Ok(stream)
}
```

Implementation is based on [warp's](https://github.com/seanmonstar/warp/blob/master/src/filters/sse.rs)
2021-08-01 21:49:17 +02:00
David Pedersen c232c56de0
Mention required dependencies in docs (#77)
Fixes https://github.com/tokio-rs/axum/issues/70
2021-08-01 21:33:55 +02:00
David Pedersen 69ae7a686a
Fix websockets failing on Firefox (#76)
Axum expected the `Connection` header to be _exactly_ `upgrade`. Turns
out thats a bit too strict as this didn't work in Firefox.

Turns out `Connection` just has to contain `upgrade`. At least that is
what [warp does](https://github.com/seanmonstar/warp/blob/master/src/filters/ws.rs#L46).
2021-08-01 21:00:38 +02:00
David Pedersen 2cf28c6794
Improve error message of `MissingExtension` rejections (#72)
Now includes the name of missing type.
2021-08-01 15:50:57 +02:00
David Pedersen 6f30d4aa6a
Improve documentation for router (#71)
Fixes #67
2021-08-01 15:42:50 +02:00
David Pedersen f581e3efb2
Clarify required response body type when routing to `tower::Service`s (#69) 2021-08-01 15:42:12 +02:00
David Pedersen f67abd1ee2
Add extractor for remote connection info (#55)
Fixes https://github.com/tokio-rs/axum/issues/43

With this you can get the remote address like so:

```rust
use axum::{prelude::*, extract::ConnectInfo};
use std::net::SocketAddr;

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

async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
    format!("Hello {}", addr)
}

// Starting the app with `into_make_service_with_connect_info` is required
// for `ConnectInfo` to work.
let make_svc = app.into_make_service_with_connect_info::<SocketAddr, _>();

hyper::Server::bind(&"0.0.0.0:3000".parse().unwrap())
    .serve(make_svc)
    .await
    .expect("server failed");
```

This API is fully generic and supports whatever transport layer you're using with Hyper. I've updated the unix domain socket example to extract `peer_creds` and `peer_addr`.
2021-07-31 21:36:30 +02:00
David Pedersen 407aa533d7
Return `405 Method Not Allowed` for unsupported method for route (#63)
Fixes https://github.com/tokio-rs/axum/issues/61
2021-07-31 21:05:53 +02:00
David Pedersen 5407247e90
Implement `Deref` for extractors (#56)
Fixes https://github.com/tokio-rs/axum/issues/54
2021-07-31 14:54:10 +02:00
David Pedersen d88212c015
Implement `Sink` and `Stream` for `WebSocket` (#52)
Among other things, this makes [`StreamExt::split`](https://docs.rs/futures/0.3.16/futures/stream/trait.StreamExt.html#method.split) accessible so one can read and write at the same time.
2021-07-31 10:51:41 +02:00
David Pedersen 6c1279a415 Version 0.1.1 2021-07-30 17:20:38 +02:00
David Pedersen ca34b4184a Update changelog for 0.1.0 release 2021-07-30 17:17:25 +02:00
David Pedersen 002e3f92b3
Misc repo setup (#7) 2021-06-12 20:18:21 +02:00