feat: fix CORS

This commit is contained in:
2024-09-24 17:54:02 +02:00
parent 0aa0aebbad
commit 8700354ad2
6 changed files with 28 additions and 5 deletions

View File

@ -21,6 +21,7 @@ thiserror.workspace = true
entity = { path = "../../entity" }
migration = { path = "../../migration" }
utils = { path = "../utils" }
tower-http = { version = "0.6.1", features = ["cors"] }
[dev-dependencies]
cargo-watch = "8.5.2"

View File

@ -24,5 +24,5 @@ ln -s ../../.env .env
Pour lancer le serveur en mode développement, exécutez la commande suivante :
```bash
systemfd --no-pid -s http::3030 -- cargo watch -x 'run --bin backend'
systemfd --no-pid -s http::8080 -- cargo watch -x 'run --bin backend'
```

View File

@ -1,9 +1,10 @@
use anyhow::Error as AnyError;
use axum::http::{StatusCode, Uri};
use axum::http::{header, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use axum::{routing::get, Router};
use sea_orm::{DatabaseConnection, DbErr};
use thiserror::Error;
use tower_http::cors::{Any, CorsLayer};
use ::utils::config::{load_config, ConfigError};
@ -30,11 +31,17 @@ pub async fn get_router() -> Result<Router, DbErr> {
let db_connection = db::get_connection().await?;
let state: AppState = AppState { db_connection };
let cors = CorsLayer::new()
.allow_methods(Any)
.allow_origin(Any)
.allow_headers([header::CONTENT_TYPE]);
Ok(Router::new()
.route("/", get(|| async { "Hello, world!" }))
.merge(api::get_routes())
.fallback(fallback)
.with_state(state))
.with_state(state)
.layer(cors))
}
async fn fallback(uri: Uri) -> (StatusCode, String) {

View File

@ -32,7 +32,7 @@ async fn main() -> Result<(), BackendError> {
};
let local_addr = listener.local_addr()?;
println!("Listening on {}", local_addr);
println!("Listening on http://{}", local_addr);
axum::serve(listener, app).await?;
Ok(())