Commit Graph
111 Commits
Author SHA1 Message Date
ImbolcandGitHub 4be4e1d17c Fix doc typo (#342) 2021-09-22 14:07:26 +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
David PedersenandGitHub e698586193 Document adding middleware to multiple groups of routes (#293) 2021-08-31 07:07:57 +00: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 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 Pedersen 0ab6ea6b6a Mention tower-log feature in docs 2021-08-23 18:40:18 +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 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 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 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 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 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 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
Eduardo CanellasandGitHub 57e440ed2e move relevant docs sections to be under "Routing" (#175) 2021-08-16 09:17:26 +02:00
Kai JewsonandGitHub 9cd543401f Implement SSE using responses (#98) 2021-08-14 17:29:09 +02:00
David PedersenandGitHub 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 PedersenandGitHub 75b5615ccd Add axum::Error (#150)
Replace `BoxStdError` and supports downcasting
2021-08-07 19:56:44 +02:00
David PedersenandGitHub 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 PedersenandGitHub 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 PedersenandGitHub 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
SunliandGitHub 345163e98d Common JSON wrapper type for response and request (#140) 2021-08-07 16:07:13 +02:00
Jonas PlatteandGitHub 6a042a9b3a Cleanup CI (#141)
* Feature-gate test that depends on non-default features

Makes `cargo check` work without extra flags.

* Don't set doc(html_root_url)

It is no longer recommended:
https://github.com/rust-lang/api-guidelines/pull/230

* Remove documentation URL from Cargo.toml

crates.io will link to the right version on docs.rs automatically.

* Ensure toolchains installed by actions-rs/toolchain are actually used

* Fix missing rustup component in check job

* Raise MSRV to 1.51

Older versions weren't actually working before.

* Only run clippy & rustfmt on stable toolchain

MSRV is checked in test-versions.

* Allow cargo doc to succeed without headers and multipart features

CI will still ensure that intra-doc links that rely on these are not broken.
2021-08-07 11:06:42 +02:00
David PedersenandGitHub 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
SunliandGitHub 9fdbd42fba Implement path extractor (#124)
Fixes #42
2021-08-06 10:17:57 +02:00
David PedersenandGitHub 5c12328892 Replace hyper::Server with axum::Server in docs (#118)
* Replace `hyper::Server` with `axum::Server` in docs

* Change readme as well
2021-08-04 15:38:51 +02:00
David Pedersen cffdedc055 Move comments in docs outside code block 2021-08-04 15:07:04 +02:00
David Pedersen 96fac52519 Make docs on required deps more clear 2021-08-04 15:06:47 +02:00
7cf8dafdce Re-export http crate and hyper::Server (#110)
Co-Authored-By: David Pedersen <[email protected]>

Co-authored-by: David Pedersen <[email protected]>
2021-08-04 12:29:42 +02:00
Jonas PlatteandGitHub d285dfb568 Tell clippy about MSRV (#114)
* Remove unused import

* Tell clippy about MSRV
2021-08-04 12:15:58 +02:00
Jonas PlatteandGitHub 015f6e0c21 Fix typos found by typos-cli (#113) 2021-08-04 12:09:39 +02:00
PatatasDelPapaandGitHub 715e624d8c Remove unused imports from example (#104)
Remove unused imports from the first crate documentation example.
2021-08-03 21:55:27 +02:00
David PedersenandGitHub 55c1a29420 Version 0.1.2 (#80) 2021-08-01 22:13:43 +02:00
David PedersenandGitHub 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 PedersenandGitHub 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 PedersenandGitHub 6f30d4aa6a Improve documentation for router (#71)
Fixes #67
2021-08-01 15:42:50 +02:00
David PedersenandGitHub f581e3efb2 Clarify required response body type when routing to tower::Services (#69) 2021-08-01 15:42:12 +02:00
David PedersenandGitHub 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
Eduardo CanellasandGitHub 4fbc99c6ef docs: fix typo (#45) 2021-07-31 00:06:27 +02:00
David Pedersen 6c1279a415 Version 0.1.1 2021-07-30 17:20:38 +02:00
David Pedersen 94d2b5f8a6 Misc readme/docs improvements 2021-07-30 15:51:59 +02:00
David PedersenandGitHub d843f4378b Make websocket handlers support extractors (#41) 2021-07-30 15:19:53 +02:00
David Pedersen d927c819d3 Clarify docs around body extractors 2021-07-23 00:27:08 +02:00
David Pedersen ba9c03d146 Update links 2021-07-22 19:39:08 +02:00
David Pedersen 7c37bb818a Update goals in readme 2021-07-22 15:44:59 +02:00
David Pedersen e4a0199c76 Add missing TOC link 2021-07-22 15:38:55 +02:00