tower-rs / tower-http

HTTP specific Tower utilities.
675 stars 156 forks source link

CorsLayer not working with axum. #439

Closed chapuzzo closed 9 months ago

chapuzzo commented 9 months ago

Bug Report

CorsLayer is not working with axum at it did before.

Version

0.5.0

Platform

Darwin my-laptop.local 22.6.0 Darwin Kernel Version 22.6.0: Fri Sep 15 13:39:52 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_X86_64 x86_64

Description

Before last update I was able to use cors as shown, now compilation gives the error below. 0.4.4 worked as expected.

I tried this code:

use axum::{routing::get, Router, Server};
use tower_http::cors::CorsLayer;

#[tokio::main]
async fn main() -> Result<(), ()> {
    let address = "0.0.0.0:3000".parse().unwrap();
    let cors_layer = CorsLayer::very_permissive();
    let router = Router::new()
    .route("/", get(root))
    .layer(cors_layer.into());

    Server::bind(&address)
        .serve(router.into_make_service())
        .await
        .unwrap();

    Ok(())
}

async fn root() -> &'static str {
    "Hello worlds"
}

I expected to see this happen:

Instead, this happened:

error[E0277]: the trait bound `Cors<Route<_>>: tower_service::Service<axum::http::Request<_>>` is not satisfied
   --> src/bin/demo.rs:10:12
    |
10  |     .layer(cors_layer);
    |      ----- ^^^^^^^^^^ the trait `tower_service::Service<axum::http::Request<_>>` is not implemented for `Cors<Route<_>>`
    |      |
    |      required by a bound introduced by this call
    |
    = help: the trait `tower_service::Service<http::request::Request<ReqBody>>` is implemented for `Cors<S>`
note: required by a bound in `Router::<S, B>::layer`
   --> /Users/me/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.6.20/src/routing/mod.rs:236:21
    |
233 |     pub fn layer<L, NewReqBody>(self, layer: L) -> Router<S, NewReqBody>
    |            ----- required by a bound in this associated function
...
236 |         L::Service: Service<Request<NewReqBody>> + Clone + Send + 'static,
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Router::<S, B>::layer`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `server` (bin "demo") due to previous error

other crates involved:

seanmonstar commented 9 months ago

Duplicate of #438.