Commit Graph
89 Commits
Author SHA1 Message Date
David Pedersen 02a035fb14 Add MatchedPath extractor (#412)
Fixes #386
2021-10-25 21:38:29 +00:00
David Pedersen 47017f90b3 Support deserializing i128 and u128 in extract::Path (#406)
Fixes https://github.com/tokio-rs/axum/issues/398
2021-10-24 20:27:51 +00:00
David Pedersen 7692baf837 Reorganize method routers for handlers and services (#405)
* Re-organize method routing for handlers

* Re-organize method routing for services

* changelog
2021-10-24 20:05:16 +00:00
David PedersenandJonas Platte 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 <[email protected]>

* 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 <[email protected]>
2021-10-24 15:22:49 +02:00
David Pedersen 9fcc884374 Change Connected::connect_info to return Self (#396)
I've been thinking that having an associated type probably isn't
necessary. I imagine most users are either using `SocketAddr` to the
remote connection IP, or writing their own connection struct.
2021-10-19 23:06:15 +02:00
David Pedersen fe4c0ae386 Remove some #[allow(warnings)] (#376)
These were probably added during development and then forgotten to be
removed.
2021-10-08 14:09:44 +00: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 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 Pedersen 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 Pedersen 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 Pinon 0c18caa10f Add accessors to TypedHeaderRejection fields (#317)
Fixes #316
2021-09-12 16:11:51 +00:00
heliumbrain 3741e16cf2 Fix typos in docs (#318)
* Fix typo in docs

Typos for FromRequest

* One more typo
2021-09-12 14:25:25 +00:00
David Pedersen 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-Miura c7c73a0d72 Fix a typo in documentation (#280) 2021-08-27 20:20:59 +02:00
David Pedersen 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 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 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 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 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 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 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
Florian Thelliez d9a06ef14b Remove axum::prelude (#195) 2021-08-18 00:04:15 +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 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 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
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 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 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 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
Sunli 345163e98d Common JSON wrapper type for response and request (#140) 2021-08-07 16:07:13 +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 904227419c Fix tests for 1.51
They used array `IntoIterator`.
2021-08-07 11:08:20 +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 9fdbd42fba Implement path extractor (#124)
Fixes #42
2021-08-06 10:17:57 +02:00