CORS support
Some checks failed
Rust Build / Check (pull_request) Successful in 50s
Rust Build / Test Suite (pull_request) Successful in 1m32s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Successful in 52s
Rust Build / build (pull_request) Successful in 1m34s

This commit is contained in:
2025-09-29 17:48:49 -04:00
parent 80cbaeb419
commit 92f24d4c11

View File

@@ -51,32 +51,47 @@ mod init {
// use axum::routing::get; // use axum::routing::get;
// use axum::routing::Router; // use axum::routing::Router;
pub fn configure_cors() -> tower_http::cors::CorsLayer { pub async fn configure_cors() -> tower_http::cors::CorsLayer {
// Start building the CORS layer with common settings // Start building the CORS layer with common settings
let cors = tower_http::cors::CorsLayer::new() let cors = tower_http::cors::CorsLayer::new()
.allow_methods([axum::http::Method::GET, axum::http::Method::POST, axum::http::Method::PUT, axum::http::Method::DELETE]) // Specify allowed methods:cite[2] .allow_methods([
.allow_headers([axum::http::header::CONTENT_TYPE, axum::http::header::AUTHORIZATION]) // Specify allowed headers:cite[2] axum::http::Method::GET,
axum::http::Method::POST,
axum::http::Method::PUT,
axum::http::Method::DELETE,
]) // Specify allowed methods:cite[2]
.allow_headers([
axum::http::header::CONTENT_TYPE,
axum::http::header::AUTHORIZATION,
]) // Specify allowed headers:cite[2]
.allow_credentials(true) // If you need to send cookies or authentication headers:cite[2] .allow_credentials(true) // If you need to send cookies or authentication headers:cite[2]
.max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2] .max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2]
// Dynamically set the allowed origin based on the environment // Dynamically set the allowed origin based on the environment
match std::env::var("ENVIRONMENT").as_deref() { match std::env::var(icarus_envy::keys::APP_ENV).as_deref() {
Ok("production") => { Ok("production") => {
// In production, allow only your specific, trusted origins // In production, allow only your specific, trusted origins
let allowed_origins_env = icarus_envy::environment::get_allowed_origins().await;
// let allowed_origins: Vec<String> = allowed_origins_env.split(",").map(|s| s.to_string()).collect();
let allowed_origins: Vec<axum::http::HeaderValue> = allowed_origins_env.split(",").map(|s| s.parse::<axum::http::HeaderValue>().unwrap()).collect();
cors.allow_origin(allowed_origins)
// cors.allow_origin(vec![""])
/*
cors.allow_origin(vec![ cors.allow_origin(vec![
"https://www.your-production-domain.com".parse::<axum::http::HeaderValue>().unwrap(), "https://www.your-production-domain.com"
.parse::<axum::http::HeaderValue>()
.unwrap(),
"https://your-production-domain.com".parse().unwrap(), "https://your-production-domain.com".parse().unwrap(),
]) ])
} */
Ok("staging") => {
// Staging environment
cors.allow_origin("https://staging.your-domain.com".parse::<axum::http::HeaderValue>().unwrap())
} }
_ => { _ => {
// Development (default): Allow localhost origins // Development (default): Allow localhost origins
cors.allow_origin(vec![ cors.allow_origin(vec![
"http://localhost:3000".parse().unwrap(), "http://localhost:8000".parse().unwrap(),
"http://127.0.0.1:3000".parse().unwrap(), "http://127.0.0.1:8000".parse().unwrap(),
"http://localhost:4200".parse().unwrap(),
"http://127.0.0.1:4200".parse().unwrap(),
]) ])
} }
} }
@@ -110,7 +125,7 @@ mod init {
callers::endpoints::REFRESH_TOKEN, callers::endpoints::REFRESH_TOKEN,
post(callers::login::endpoint::refresh_token), post(callers::login::endpoint::refresh_token),
) )
.layer(cors::configure_cors()) .layer(cors::configure_cors().await)
} }
pub async fn app() -> Router { pub async fn app() -> Router {