Skip to content

Provide example on how to use hyper 1.0.0 with tower layers #3154

Open

Description

@o-agassizii

Version
1.0.0-rc.3

Platform
Not relevant

Description
Can some of the examples for hyper 1.0.0 show how to make use of tower layers with hyper 1.0.0-rc.3?

All of the examples I can find online where people are using older versions of hyper with tower rely on the higher level Server API.

How do you use the lower level hyper 1.0.0 API like in https://hyper.rs/guides/1/server/hello-world/

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    // We create a TcpListener and bind it to 127.0.0.1:3000
    let listener = TcpListener::bind(addr).await?;

    // We start a loop to continuously accept incoming connections
    loop {
        let (stream, _) = listener.accept().await?;

        // Spawn a tokio task to serve multiple connections concurrently
        tokio::task::spawn(async move {
            // Finally, we bind the incoming connection to our `hello` service
            if let Err(err) = http1::Builder::new()
                // `service_fn` converts our function in a `Service`
                .serve_connection(stream, service_fn(hello))
                .await
            {
                println!("Error serving connection: {:?}", err);
            }
        });
    }
}

with tower layers?

Activity

  1. added
    C-bugCategory: bug. Something is wrong. This is bad!
    on Mar 1, 2023
  2. o-agassizii commented on Mar 16, 2023

    @o-agassizii
    Author

    Anyone? @seanmonstar

  3. seanmonstar commented on Mar 16, 2023

    @seanmonstar
    Member

    What have you tried so far? Where do you get stumped? The code you pasted is the hello world example.

  4. o-agassizii commented on Mar 31, 2023

    @o-agassizii
    Author

    I tried a bunch of things, all too convoluted to meaningfully describe here.

    This is why I request that a super basic example is provided showing how tower layers can be combined with the lower level hello world example.

  5. added
    A-docsArea: documentation.
    E-easyEffort: easy. A task that would be a great starting point for a new contributor.
    and removed
    C-bugCategory: bug. Something is wrong. This is bad!
    on Apr 28, 2023
  6. Wesley-Arizio commented on Nov 26, 2023

    @Wesley-Arizio

    @seanmonstar @o-agassizii
    I have this repo with an example of an implementation of tower layer in hyper server https://github.com/Wesley-Arizio/hyper-tower, there is some stuff I need to improve but I think it's a good start.

    if you guys find that is a good solution I can open a PR with this example or if you have suggestions I can implement them (or at least try to).

  7. NewbMiao commented on Feb 1, 2024

    @NewbMiao

    Now hyper not relies on tower::service, so it's a bit annoying hard to reuse layer from tower::http. if other web framwork upgrade to latest hyper, would have same issue. right?
    feel like it's a pity to get rid of tower service

  8. seanmonstar commented on Feb 1, 2024

    @seanmonstar
    Member

    There's an adapter available in https://docs.rs/hyper-util/latest/hyper_util/service/struct.TowerToHyperService.html

    There's also an item in #3411 to add a middleware section to the guides, if anyone would like to start that.

  9. NewbMiao commented on Feb 2, 2024

    @NewbMiao

    Cool, @seanmonstar I've had a play with the TowerToHyperService.html and also raised a PR with examples for middleware docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: documentation.E-easyEffort: easy. A task that would be a great starting point for a new contributor.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions