From 560297ad880527f1a3b11101e467ef18ea450eb8 Mon Sep 17 00:00:00 2001 From: xenia Date: Sun, 1 Sep 2024 17:07:56 +0200 Subject: [PATCH] Add permissive CORS-layer to the backend --- back/Cargo.lock | 17 +++++++++++++++++ back/Cargo.toml | 1 + back/src/main.rs | 2 ++ 3 files changed, 20 insertions(+) diff --git a/back/Cargo.lock b/back/Cargo.lock index 3f07dc3..55e2c41 100644 --- a/back/Cargo.lock +++ b/back/Cargo.lock @@ -939,6 +939,7 @@ dependencies = [ "serde", "sqlx", "tokio", + "tower-http", "tracing-subscriber", ] @@ -1575,6 +1576,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" +dependencies = [ + "bitflags 2.6.0", + "bytes", + "http", + "http-body", + "http-body-util", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" diff --git a/back/Cargo.toml b/back/Cargo.toml index ced5ab8..838b816 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -8,4 +8,5 @@ axum = "0.7.5" serde = "1.0.204" sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite"] } tokio = { version = "1.38.1", features = ["rt-multi-thread", "macros", "net", "time", "sync"] } +tower-http = { version = "0.5.2", features = ["cors"] } tracing-subscriber = "0.3.18" diff --git a/back/src/main.rs b/back/src/main.rs index 817d936..c6e5378 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use axum::{ extract::State, http::StatusCode, routing::post, Json, Router }; +use tower_http::cors::CorsLayer; use sqlx::{Row, sqlite::{SqliteConnectOptions, SqlitePoolOptions}, Pool, Sqlite}; @@ -48,6 +49,7 @@ async fn main() { .route("/api/echo", post(echo)) .route("/api/get_counter", post(get_counter)) .route("/api/set_counter", post(set_counter)) + .layer(CorsLayer::permissive()) .with_state(state); axum::serve(listener, app)