bloom_web_core/
config.rs

1use config::{Config, ConfigError, File};
2use serde::Deserialize;
3
4/// Configuration settings for Cross-Origin Resource Sharing (CORS).
5///
6/// This struct defines all the CORS-related settings that can be configured
7/// for the web server to control cross-origin requests.
8#[derive(Debug, Deserialize, Default, Clone)]
9pub struct CorsSettings {
10    pub enabled: bool,
11    pub allowed_origins: Option<Vec<String>>,
12    pub allowed_methods: Option<Vec<String>>,
13    pub allowed_headers: Option<Vec<String>>,
14    pub allow_credentials: Option<bool>,
15    pub max_age: Option<u64>,
16}
17
18/// Main application settings loaded from configuration file.
19///
20/// This struct contains all the configuration parameters needed to run the application,
21/// including server port, database connection, and CORS settings.
22#[derive(Debug, Deserialize)]
23pub struct Settings {
24    pub port: u16,
25    pub database_url: String,
26    pub cors: Option<CorsSettings>,
27}
28
29impl Settings {
30    /// Loads configuration settings from the config.toml file.
31    ///
32    /// This function reads the configuration from a TOML file and deserializes
33    /// it into the Settings struct.
34    ///
35    /// # Returns
36    /// * `Result<Self, ConfigError>` - The loaded settings or a configuration error
37    pub fn load() -> Result<Self, ConfigError> {
38        let config = Config::builder()
39            .add_source(File::with_name("config.toml"))
40            .build()?;
41        config.try_deserialize()
42    }
43}