Commit graph

111 commits

Author SHA1 Message Date
David Pedersen
fc9bfb8a50
Add HTTP proxy example (#425) 2021-10-26 20:46:40 +02:00
David Pedersen
e949c47df6
Fix inconsistent naming of WebSocket rejections (#416) 2021-10-25 23:09:47 +00:00
David Pedersen
1634e67e99
Stores routes in a map (#408)
With https://github.com/tokio-rs/axum/pull/404 and https://github.com/tokio-rs/axum/pull/402 all routes now have the same types and thus we don't need to nest them but can instead store them all in a map. This simplifies the routing quite a bit and is faster as well.

High level changes:
- Routes are now stored in a `HashMap<RouteId, Route<B>>`.
- `Router::or` is renamed to `Router::merge` because thats what it does now. It copies all routes from one router to another. This also means overlapping routes will cause a panic which is nice win.
- `Router::merge` now only accepts `Router`s so added `Router::fallback` for adding a global 404 handler.
- The `Or` service has been removed.
- `Router::layer` now only adds layers to the routes you actually have meaning middleware runs _after_ routing. I believe that addresses https://github.com/tokio-rs/axum/issues/380 but will test that on another branch.
2021-10-25 20:49:39 +02:00
QP Hou
fb87a6a4d3
Add ROAPI to community showcase (#407) 2021-10-25 08:15:16 +02: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 Pedersen
0ee7379d4f
Fix compile time regression by boxing routes internally (#404)
This is a reimplementation of #401 but with the new matchit based router.

Fixes #399
2021-10-24 20:52:42 +02:00
David Pedersen
f10508db0b
Revamp error handling model (#402)
* Revamp error handling model

* changelog improvements and typo fixes

* Fix a few more Infallible bounds

* minor docs fixes
2021-10-24 17:33:03 +00: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
Eray Karatay
a724af3d0a
Add reverse-proxy example (#389) 2021-10-19 22:52:19 +02:00
silvioprog
8ca5538405
Fix typo in main.rs (#381) 2021-10-12 18:20:27 +00:00
David Pedersen
ac291baaec
Fix dead code warnings (#373) 2021-10-07 16:49:57 +02:00
Eray Karatay
b7002f68d5
Update some examples (#364) 2021-10-05 07:50:27 +02:00
David Pedersen
f8154a088c
Add example showing up to customize extractor error (#356)
Lots have been asking about this so makes sense to have an example for.

Once this is merged I'll add a link to it in the docs.
2021-09-30 17:55:58 +00:00
LT
3adc8d733d
Add validator example (#352) 2021-09-28 18:12:23 +02:00
LT
0b3ee5b2ce
Add graceful shutdown example (#349) 2021-09-28 09:08:49 +00:00
Andrei Zolkin
9df57e6ff2
Migrate all examples to use std::env::var_os (#312) 2021-09-12 17:39:43 +02:00
David Pedersen
4e088b40f6
Show how to configure TraceLayer (#314)
This isn't very obvious so makes sense to include in the example
2021-09-10 17:56:00 +00:00
silvioprog
6c13b22cd4
Add JWT example (#306) (#308)
Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
2021-09-10 17:51:20 +02:00
Andrei Zolkin
283bbfbd36
Add JWT auth example as community showcase (#309) 2021-09-08 06:15:17 +00:00
zhihanz
835432b5c9
Add datafuse as community showcase (#302) 2021-09-02 08:16:49 +02:00
Ken-Miura
e6ca9e4b04
Fix content-type in example response (#276) 2021-08-26 18:16:41 +00:00
programatik29
1a5f977896
Simplify tls-rustls example (#254)
## Motivation

Current `tls-rustls` example might be inconvenient for some people.

## Solution

Rename current example to `low-level-rustls` and add a high level example in its place.
2021-08-24 09:56:31 +02:00
Eduardo Canellas
1b5359add7
small changes to error handling example (#250) 2021-08-24 07:42:13 +02:00
David Pedersen
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 Pedersen
2bbf6105d0
Fix 404 example (#226)
Forgot to actually set the correct status code
2021-08-21 12:02:50 +02:00
David Pedersen
39a0c26795
Add print-request-response example (#216)
* Add `print-request-response` example

Someone asked about this on Discord. I think its worth adding as an
example.

* add missing feature
2021-08-20 09:26:31 +02:00
David Pedersen
1bda638c6b Simplify 404 example using or 2021-08-19 22:50:42 +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
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
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
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
b4cbd7f147
Add Redirect response (#192)
* Add `Redirect` response

* Add `Redirect::found`
2021-08-16 19:48:03 +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
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
fluunke
0674c9136a
Add oauth2 example (#144) 2021-08-08 17:22:24 +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
2389761ce7
Add dedicated tracing/logging example (#155)
Useful to link to since several have asked.
2021-08-07 22:11:55 +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
Sunli
9fdbd42fba
Implement path extractor (#124)
Fixes #42
2021-08-06 10:17:57 +02:00
David Pedersen
2be79168d8
Handle METHOD_NOT_ALLOWED in 404 example (#131)
If a route existed but had no handler for the method, the code used in
the 404 example wouldn't catch it.
2021-08-06 01:15:23 +02:00
Laurențiu Nicola
68f826ef3b
Simplify tracing-subscriber initialization (#128) 2021-08-05 19:43:03 +02:00
Grzegorz Baranski
f18e423fb0
docs: add community showcase (#126) 2021-08-05 13:47:19 +02:00