diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-08-29 17:23:59 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-08-29 17:23:59 +0200 |
commit | 7c492396257462f96aba047febf3168f8ec2524e (patch) | |
tree | 064bc7d7f9d5085f73eb6cc1c805adfe5735a905 | |
parent | 776e61237601236fb1ad68f4047cca40263acf23 (diff) |
Add CORS
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 12 |
2 files changed, 11 insertions, 3 deletions
@@ -14,7 +14,7 @@ serde_json = "1.0.125" tokio = { version = "1.38.1", features = ["rt"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } -tower-http = { version = "0.5.2", features = ["trace", "compression-br", "propagate-header", "sensitive-headers"] } +tower-http = { version = "0.5.2", features = ["trace", "cors", "compression-br", "propagate-header", "sensitive-headers"] } lazy_static = "1.5.0" serde = { version = "1.0.208", features = ["derive"] } tokio-postgres = "0.7.11" diff --git a/src/main.rs b/src/main.rs index e53395a..1614e6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,15 @@ use std::{net::SocketAddr, sync::Arc, time::Duration}; use crate::config::CONFIG; use async_graphql::{EmptySubscription, Schema}; use axum::{ - http::{header, Request}, + http::{header, Method, Request}, routing::post, Extension, Router, }; use tokio::net::TcpListener; use tower_http::{ - classify::ServerErrorsFailureClass, sensitive_headers::SetSensitiveHeadersLayer, + classify::ServerErrorsFailureClass, + cors::{Any, CorsLayer}, + sensitive_headers::SetSensitiveHeadersLayer, trace::TraceLayer, }; @@ -63,6 +65,12 @@ async fn create_app() -> Router { }, ), ) + .layer( + CorsLayer::new() + .allow_methods([Method::OPTIONS, Method::GET, Method::POST]) + .allow_headers(vec![header::CONTENT_TYPE, header::AUTHORIZATION]) + .allow_origin(Any), + ) .layer(Extension(state)) } |