bloom-web-core 0.1.1

Core functionality for the Bloom web framework
use config::{Config, ConfigError, File};
use serde::Deserialize;

/// Configuration settings for Cross-Origin Resource Sharing (CORS).
///
/// This struct defines all the CORS-related settings that can be configured
/// for the web server to control cross-origin requests.
#[derive(Debug, Deserialize, Default, Clone)]
pub struct CorsSettings {
    pub enabled: bool,
    pub allowed_origins: Option<Vec<String>>,
    pub allowed_methods: Option<Vec<String>>,
    pub allowed_headers: Option<Vec<String>>,
    pub allow_credentials: Option<bool>,
    pub max_age: Option<u64>,
}

/// Main application settings loaded from configuration file.
///
/// This struct contains all the configuration parameters needed to run the application,
/// including server port, database connection, and CORS settings.
#[derive(Debug, Deserialize)]
pub struct Settings {
    pub port: u16,
    pub database_url: String,
    pub cors: Option<CorsSettings>,
}

impl Settings {
    /// Loads configuration settings from the config.toml file.
    ///
    /// This function reads the configuration from a TOML file and deserializes
    /// it into the Settings struct.
    ///
    /// # Returns
    /// * `Result<Self, ConfigError>` - The loaded settings or a configuration error
    pub fn load() -> Result<Self, ConfigError> {
        let config = Config::builder()
            .add_source(File::with_name("config.toml"))
            .build()?;
        config.try_deserialize()
    }
}