Reputation: 45
Im using axum and sea-orm for my backend. i got an error with CORS.
I tried to use cors layer
I dont have any idea how to fix it. I googled it, but all the other codes work well.
i did minimal reproducible example code:
use axum::Server;
use axum::{
routing::{get},
Router,
};
use http::Method;
use migration::sea_orm::DatabaseConnection;
use migration::MigratorTrait;
use migration::{
sea_orm::{Database},
Migrator,
};
use tower_http::cors::{Any, CorsLayer};
use std::{net::SocketAddr, str::FromStr};
// use ;
#[tokio::main]
async fn start() -> anyhow::Result<()> {
let server_url = ("127.0.0.1:8000").to_string();
let connection = Database::connect("sqlite://data.db?mode=rwc")
.await
.expect("Database connection failed");
Migrator::up(&connection, None).await.unwrap();
let state = AppState { connection };
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any);
let app: Router = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.layer(cors)
.with_state(state);
let addr = SocketAddr::from_str(&server_url).unwrap();
Server::bind(&addr).serve(app.into_make_service()).await?;
Ok(())
}
#[derive(Clone)]
pub struct AppState {
pub(crate) connection: DatabaseConnection,
}
fn main() {
start();
}
and error: the trait bound `Cors<Route<_>>: tower_service::Service<axum::http::Request<_>>` is not satisfied the trait `tower_service::Service<http::Request<ReqBody>>` is implemented for `Cors<S>
[dependencies]
tokio = { version = "1.29.0", features = ["full"] }
axum = { version = "0.6.19", features = ["macros"] }
tower-http = { version = "0.5.2", features = ["fs", "cors"] }
anyhow = "1.0.71"
migration = { path = "./migration" }
sea-orm = "1.0.0"
http = "1.0.0"
Upvotes: 0
Views: 163
Reputation: 45
The solution to this problem is to update axum to version 0.7.5
axum = { version = "0.7.5", features = ["macros"] }
Upvotes: 1