feat: fix CORS

This commit is contained in:
Florian Briand 2024-09-24 17:54:02 +02:00
parent 0aa0aebbad
commit 8700354ad2
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
6 changed files with 28 additions and 5 deletions

15
Cargo.lock generated
View File

@ -573,6 +573,7 @@ dependencies = [
"systemfd", "systemfd",
"thiserror", "thiserror",
"tokio", "tokio",
"tower-http",
"utils", "utils",
] ]
@ -6344,6 +6345,20 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "tower-http"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97"
dependencies = [
"bitflags 2.6.0",
"bytes",
"http",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.3" version = "0.3.3"

View File

@ -75,7 +75,7 @@ Pour lancer l'application en mode développement, il est nécessaire d'exécuter
```bash ```bash
# Lancement du serveur backend # Lancement du serveur backend
systemfd --no-pid -s http::3030 -- cargo watch -x 'run --bin backend' systemfd --no-pid -s http::8080 -- cargo watch -x 'run --bin backend'
``` ```
```bash ```bash

View File

@ -21,6 +21,7 @@ thiserror.workspace = true
entity = { path = "../../entity" } entity = { path = "../../entity" }
migration = { path = "../../migration" } migration = { path = "../../migration" }
utils = { path = "../utils" } utils = { path = "../utils" }
tower-http = { version = "0.6.1", features = ["cors"] }
[dev-dependencies] [dev-dependencies]
cargo-watch = "8.5.2" 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 : Pour lancer le serveur en mode développement, exécutez la commande suivante :
```bash ```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 anyhow::Error as AnyError;
use axum::http::{StatusCode, Uri}; use axum::http::{header, StatusCode, Uri};
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use sea_orm::{DatabaseConnection, DbErr}; use sea_orm::{DatabaseConnection, DbErr};
use thiserror::Error; use thiserror::Error;
use tower_http::cors::{Any, CorsLayer};
use ::utils::config::{load_config, ConfigError}; 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 db_connection = db::get_connection().await?;
let state: AppState = AppState { db_connection }; let state: AppState = AppState { db_connection };
let cors = CorsLayer::new()
.allow_methods(Any)
.allow_origin(Any)
.allow_headers([header::CONTENT_TYPE]);
Ok(Router::new() Ok(Router::new()
.route("/", get(|| async { "Hello, world!" })) .route("/", get(|| async { "Hello, world!" }))
.merge(api::get_routes()) .merge(api::get_routes())
.fallback(fallback) .fallback(fallback)
.with_state(state)) .with_state(state)
.layer(cors))
} }
async fn fallback(uri: Uri) -> (StatusCode, String) { async fn fallback(uri: Uri) -> (StatusCode, String) {

View File

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