feat: fix CORS
This commit is contained in:
parent
0aa0aebbad
commit
8700354ad2
15
Cargo.lock
generated
15
Cargo.lock
generated
@ -573,6 +573,7 @@ dependencies = [
|
||||
"systemfd",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tower-http",
|
||||
"utils",
|
||||
]
|
||||
|
||||
@ -6344,6 +6345,20 @@ dependencies = [
|
||||
"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]]
|
||||
name = "tower-layer"
|
||||
version = "0.3.3"
|
||||
|
@ -75,7 +75,7 @@ Pour lancer l'application en mode développement, il est nécessaire d'exécuter
|
||||
|
||||
```bash
|
||||
# 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
|
||||
|
@ -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"
|
||||
|
@ -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'
|
||||
```
|
||||
|
@ -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) {
|
||||
|
@ -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(())
|
||||
|
Loading…
Reference in New Issue
Block a user