From 3f0efabcdc8fd12535c29acc734474e093e6f2f5 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 13 Sep 2022 17:57:02 +0200 Subject: Add CORS --- src/main.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main.rs') 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), + ) } -- cgit v1.2.3-71-g8e6c