warp icon indicating copy to clipboard operation
warp copied to clipboard

Allow passing constructed routes to another function (expose IsReject)

Open camelop opened this issue 4 years ago • 4 comments

Is your feature request related to a problem? Please describe. In our application, we construct different routes and start TLS server in different places, the resulting code looks like:

async fn foo_1(...) {
    let routes = .....
    warp::serve(routes)
      .tls()
      .key_path("...")
      .cert_path("...")
      .client_auth_required_path("...")
      .run(([0, 0, 0, 0], port))
      .await;
}

async fn foo_2(...) {
    let routes = ..... // something different
    warp::serve(routes)
      .tls()
      .key_path("...")
      .cert_path("...")
      .client_auth_required_path("...")
      .run(([0, 0, 0, 0], port))
      .await;
}

However, we want to simplify the code by constructing a separate function that looks like:

async fn warp_serve<F>(routes: F)
    where
        F: Filter + Clone + Send + Sync + 'static,
        F::Extract: Reply,
        F::Error: IsReject,
    {
    warp::serve(routes)
      .tls()
      .key_path("...")
      .cert_path("...")
      .client_auth_required_path("...")
      .run(([0, 0, 0, 0], port))
      .await;
    }

Then we can simplify the code in the beginning to be

fn foo_1(...) {
    let routes = .....
    warp_serve(routes).await;
}

fn foo_2(...) {
    let routes = ..... // something different
    warp_serve(routes).await;
}

But we cannot do that because "trait IsReject is private" so warp_serve cannot compile.

Describe the solution you'd like A clear and concise description of what you want to happen: Make IsReject a public trait.

Describe alternatives you've considered: N/A

Additional context: N/A

camelop avatar Sep 21 '21 00:09 camelop

Managed to work around this [at a slight performance penalty] by returning a BoxedFilter<(warp::reply::Response,)> from the route constructor and doing a {...}.map(Reply::into_response).boxed() :)

char avatar Dec 31 '21 11:12 char

Managed to work around this [at a slight performance penalty] by returning a BoxedFilter<(warp::reply::Response,)> from the route constructor and doing a {...}.map(Reply::into_response).boxed() :)

Thanks a lot for providing the solution and happy new year!

While in our internal project development we are doing some similar workarounds, the change of just making the trait public might be more direct and does not harm the purpose of defensive programming. I would probably send a PR myself on this later and hopefully, the dev team can also help with this.

camelop avatar Jan 01 '22 07:01 camelop

Fix for this problem? https://github.com/seanmonstar/warp/issues/944

chpio avatar Jun 29 '22 21:06 chpio

@chpio they are seeking to accept routes while I am seeking to return routes, but indeed making a public Route(s?) abstraction fixes both without/instead of making IsReject public. I could PR, but it's unclear to me which is the better long term evolution public surface area change.

qm3ster avatar Aug 30 '23 15:08 qm3ster