//! Reusable middleware for Axum applications

pub mod request_id;
pub mod logging;

pub use request_id::{RequestIdLayer, add_request_id, RequestId, REQUEST_ID_HEADER};
pub use logging::{LoggingLayer, log_request};

use tower_http::cors::{Any, CorsLayer};

/// Create a permissive CORS layer for development
pub fn permissive_cors() -> CorsLayer {
    CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any)
        .allow_credentials(true)
}

/// Create a CORS layer with specific allowed origins
pub fn cors_with_origins(origins: Vec<String>) -> CorsLayer {
    use tower_http::cors::AllowOrigin;

    let origins: Vec<_> = origins
        .into_iter()
        .filter_map(|o| o.parse().ok())
        .collect();

    CorsLayer::new()
        .allow_origin(AllowOrigin::list(origins))
        .allow_methods(Any)
        .allow_headers(Any)
        .allow_credentials(true)
}
