summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-13 15:57:02 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-13 15:57:02 +0000
commit3f0efabcdc8fd12535c29acc734474e093e6f2f5 (patch)
tree79010c9a3877a849cea30264ded7ac5a5d304888
parentf3aa3792255652371ceea6ec032c8962061eb3dc (diff)
Add CORS
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs23
2 files changed, 21 insertions, 4 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1bca769..e06a5e2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,7 +16,7 @@ serde_json = "1.0"
tokio = { version = "1.20", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
-tower-http = { version = "0.3.4", features = ["trace", "compression-br", "propagate-header", "sensitive-headers"] }
+tower-http = { version = "0.3.4", features = ["trace", "cors", "compression-br", "propagate-header", "sensitive-headers"] }
sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres", "chrono", "json" ] }
sha256 = "1.0.3"
validator = { version = "0.16.0", features = ["derive"] }
diff --git a/src/main.rs b/src/main.rs
index 39c6738..8706153 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,14 +10,19 @@ mod routes;
use crate::config::CONFIG;
use axum::{
handler::Handler,
- http::{header, Request},
+ http::{header, Method, Request},
routing::get,
Router,
};
+
use std::net::{SocketAddr, ToSocketAddrs};
use std::time::Duration;
-use tower_http::sensitive_headers::SetSensitiveHeadersLayer;
-use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
+use tower_http::{
+ classify::ServerErrorsFailureClass,
+ cors::{Any, CorsLayer},
+ sensitive_headers::SetSensitiveHeadersLayer,
+ trace::TraceLayer,
+};
use tracing::Span;
/// Main application, called by the execution of the software
@@ -78,4 +83,16 @@ async fn create_app() -> Router {
},
),
)
+ .layer(
+ CorsLayer::new()
+ .allow_methods([
+ Method::OPTIONS,
+ Method::GET,
+ Method::POST,
+ Method::PUT,
+ Method::DELETE,
+ ])
+ .allow_headers(vec![header::CONTENT_TYPE])
+ .allow_origin(Any),
+ )
}