From db4a58c3e6e2afaa42d9350eff2088e7ce18f1cc Mon Sep 17 00:00:00 2001 From: Sidharth-Singh10 Date: Sat, 28 Sep 2024 21:14:58 +0530 Subject: [PATCH] Default ALLOWED_ORIGINS to localhost:5173 with trace logging --- src/main.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index d868c00..9472392 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use socketioxide::{ use sqlx::PgPool; use tokio::net::TcpListener; use tower_http::cors::{AllowOrigin, CorsLayer}; +use tracing::warn; use tracing_subscriber::FmtSubscriber; #[tokio::main] @@ -37,7 +38,10 @@ async fn main() -> Result<(), Box> { io.ns("/", on_connect); // Get the allowed origins from the .env file - let allowed_origins = env::var("ALLOWED_ORIGINS").expect("ALLOWED_ORIGINS must be set"); + let allowed_origins = env::var("ALLOWED_ORIGINS").unwrap_or_else(|_| { + warn!("ALLOWED_ORIGINS not found in .env, defaulting to http://localhost:5173"); + "http://localhost:5173".to_string() + }); // Split the origins by comma and collect them into a vector let origins: Vec = allowed_origins @@ -49,12 +53,9 @@ async fn main() -> Result<(), Box> { let allow_origin = AllowOrigin::list(origins.iter().map(|origin| origin.parse().unwrap())); // Create a CORS layer - let cors = CorsLayer::new().allow_origin(allow_origin).allow_methods([ - Method::GET, - Method::POST, - Method::PUT, - Method::DELETE, - ]); + let cors = CorsLayer::new() + .allow_origin(allow_origin) + .allow_methods([Method::GET, Method::POST]); let app = Router::new().layer(layer).layer(cors);