Commit graph

275 commits

Author SHA1 Message Date
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
Spencer Gilbert
3cd0c0fd45
Set RUST_LOG environment var for all examples using tracing (#123)
* Set RUST_LOG environment var for all examples using tracing

Signed-off-by: Spencer Gilbert <spencer.gilbert@gmail.com>

* Update examples/multipart_form.rs

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-08-05 11:25:03 +02:00
David Pedersen
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
Sunli
09ecd42b32
Add async-graphql example (#93)
Fixes https://github.com/tokio-rs/axum/issues/68
2021-08-04 12:10:20 +02:00
Jonas Platte
015f6e0c21
Fix typos found by typos-cli (#113) 2021-08-04 12:09:39 +02:00
Grzegorz Baranski
4e9b38ddf9
Use tuple destructuring in chat example (#105) 2021-08-03 22:21:18 +02:00
Mateusz Kieblesz
b0457c08e8
Add 404 page example (#97) 2021-08-03 17:00:21 +02:00
David Pedersen
9fbababc3a
Make it clear how to run all examples (#92)
* Handle errors in websocket example

* Make it clear how to run all examples
2021-08-02 23:09:09 +02:00
Javier Viola
c6b7ad0f33
Add chat example documentation. (#85) 2021-08-02 21:42:10 +02:00
programatik29
ffefb3455c
Add chat example (#78) 2021-08-01 22:42:34 +02:00
David Pedersen
666d088b26
Including tracing setup in all examples (#79) 2021-08-01 22:01:33 +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
593f3e115f Clean up TLS example 2021-08-01 13:48:39 +02:00
David Pedersen
ea82acd175
Add sessions and cookies examples (#65)
Uses [`async-session`](https://crates.io/crates/async-session).
2021-08-01 09:15:44 +02:00
programatik29
3b579c7215
Add TLS example (with rustls) (#57) 2021-08-01 08:32:47 +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
132da26d5b
Add unix domain socket example (#53)
Not actually related to Axum, can be implemented directly with Hyper, but I figure its nice to have for demonstration and might help catch accidental breaking changes in the future.
2021-07-31 12:22:38 +02:00
David Pedersen
d843f4378b
Make websocket handlers support extractors (#41) 2021-07-30 15:19:53 +02:00
David Pedersen
6d483a623a Add testing example with real HTTP server 2021-07-22 21:12:40 +02:00
David Pedersen
9a419de290 Misc clean up 2021-07-22 21:12:29 +02:00
David Pedersen
21db427077 Update examples readme 2021-07-22 15:42:04 +02:00
David Pedersen
6705e79ed7
Add todos example (#38) 2021-07-22 15:38:32 +02:00
David Pedersen
8faed8120f
Docs improvements (#37) 2021-07-22 15:00:33 +02:00
David Pedersen
f32d325e55
Make extractors easier to write (#36)
Previously extractors worked directly on `Request<B>` which meant you
had to do weird tricks like `mem::take(req.headers_mut())` to get owned
parts of the request.

This changes that instead to use a new `RequestParts` type that have
methods to "take" each part of the request. Without having to do weird
tricks.

Also removed the need to have `B: Default` for body extractors.
2021-07-22 13:23:50 +02:00
David Pedersen
028c472c84
Add Multipart extractor for consuming multipart/form-data requests (#32)
Multipart implementation on top of `multer`
2021-07-14 16:53:37 +02:00
David Pedersen
5a5710d290
Rename to axum (#28) 2021-07-09 21:36:14 +02:00
David Pedersen
c4d266e94d
Allow errors (#26)
This changes error model to actually allow errors. I think if we're going to use this for things like tonic's route we need a more flexible error handling model. The same `handle_error` adaptors are still there but services aren't required to have `Infallible` as their error type. The error type is simply propagated all the way through.
2021-07-05 16:18:39 +02:00
David Pedersen
3fc7f1880f Rename tokio-postgres example 2021-06-19 15:40:46 +02:00
David Pedersen
8d8cc3ba3d
Examples readme (#25)
* Examples readme

* link?
2021-06-19 14:06:49 +02:00
David Pedersen
44577a0108
Add testing example (#24) 2021-06-19 13:44:21 +02:00
David Pedersen
356f1c8424
Generic request body (#22)
Fixes #21
2021-06-19 12:50:33 +02:00
David Pedersen
6a16cd40ca
Add error handling and dependency injection example (#23) 2021-06-19 12:34:42 +02:00
David Pedersen
8a82fd00aa
Add bb8 connection pool example (#19)
I figure this will be quite common.
2021-06-15 22:25:41 +02:00
David Pedersen
2f6699aeae
Add HTML template example (#17) 2021-06-13 13:58:12 +02:00
David Pedersen
460828a3cb
Add versioning extractor example (#16) 2021-06-13 13:50:56 +02:00
David Pedersen
1002685a20
Rename to awebframework (#13) 2021-06-13 11:22:02 +02:00
David Pedersen
27ebb3db7a
Add Form extractor (#12)
Fixes https://github.com/davidpdrsn/tower-web/issues/2
2021-06-13 11:01:40 +02:00
David Pedersen
7a051eb2d0
Fix static file serving in examples (#10) 2021-06-13 00:17:17 +02:00
David Pedersen
04d62798b6
Reduce body boxing (#9)
Previously, when routing between one or two requests the two body types
would be merged by boxing them. This isn't ideal since it introduces a
layer indirection for each route.

We can't require the services to be routed between as not all services
use the same body type.

This changes that so it instead uses an `Either` enum that implements
`http_body::Body` if each variant does. Will reduce the overall
allocations and hopefully the compiler can optimize things if both
variants are the same.
2021-06-12 23:59:18 +02:00
David Pedersen
b3bc4e024c
Add RoutingDsl::{serve, into_make_service} (#8) 2021-06-12 21:44:40 +02:00
David Pedersen
c9c507aece
Add support for websockets (#3)
Basically a copy/paste of whats in warp.

Example usage:

```rust
use tower_web::{prelude::*, ws::{ws, WebSocket}};

let app = route("/ws", ws(handle_socket));

async fn handle_socket(mut socket: WebSocket) {
    while let Some(msg) = socket.recv().await {
        let msg = msg.unwrap();
        socket.send(msg).await.unwrap();
    }
}
```
2021-06-12 20:50:30 +02:00
David Pedersen
c91dc7ce29 Make Request<Body> an extractor 2021-06-09 09:42:06 +02:00
David Pedersen
90c3e5ba74 Parameterize ContentLengthLimit 2021-06-09 08:14:20 +02:00
David Pedersen
1f8b39f05d More docs and expand key_value_store example 2021-06-08 12:43:16 +02:00
David Pedersen
d8b0153939 Write some more docs 2021-06-07 15:45:19 +02:00
David Pedersen
21c96e0aa1 Write a few docs 2021-06-06 23:58:44 +02:00
David Pedersen
58c18c2563 Static file serving 2021-06-06 21:53:22 +02:00
David Pedersen
470d6ceabd Add prelude 2021-06-06 11:42:44 +02:00
David Pedersen
46398afc72 Move things around a bit 2021-06-06 11:37:08 +02:00
David Pedersen
c3977d0b71 Change routing DSL 2021-06-04 01:00:48 +02:00
David Pedersen
08c10fe58d Tuple structs are cool 2021-06-01 15:07:16 +02:00
David Pedersen
ea582ab8d9 Quality of life improvements 2021-06-01 14:52:18 +02:00
David Pedersen
f690e74275 Support nesting services with error handling 2021-06-01 11:23:56 +02:00
David Pedersen
0e38037c74 More error handling 2021-06-01 00:37:07 +02:00
David Pedersen
18f613ff98 This changes everything 2021-05-31 22:54:21 +02:00
David Pedersen
d33be9683c Don't force handlers to return Results 2021-05-31 20:42:57 +02:00
David Pedersen
f6b1a6f435 More work 2021-05-31 16:28:26 +02:00
David Pedersen
6822766165 Add Html response type 2021-05-31 10:20:07 +02:00
David Pedersen
0b2f791bf4 Typed url param extractor 2021-05-30 16:53:39 +02:00
David Pedersen
03fb15e7a7 Changes to UrlParamsMap 2021-05-30 16:37:27 +02:00
David Pedersen
763d4e8d21 Routing with dynamic parts! 2021-05-30 15:44:26 +02:00
David Pedersen
7328127a3d Add example 2021-05-30 14:33:36 +02:00