parsing environment variables
This commit is contained in:
@@ -15,20 +15,21 @@ pub struct AppSettings {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
|
||||
pub struct ServerSettings {
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new() -> Result<Self, config::ConfigError> {
|
||||
// Load .env file first
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
// Merge settings from multiple sources
|
||||
let builder = config::Config::builder()
|
||||
.add_source(config::File::with_name("Settings").required(false)) // Settings.toml optional
|
||||
.add_source(Environment::default().separator("_"));
|
||||
.add_source(File::with_name("Settings").required(false))
|
||||
.add_source(
|
||||
Environment::default()
|
||||
.separator("_") // "APP" maps to struct `app`
|
||||
.try_parsing(true),
|
||||
);
|
||||
|
||||
builder.build()?.try_deserialize()
|
||||
}
|
||||
|
Reference in New Issue
Block a user