refactor: enhance env variable usage
This commit is contained in:
parent
e1c775066b
commit
0f427ba1d1
3 changed files with 21 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use secrecy::{ExposeSecret, Secret};
|
||||
use serde_aux::field_attributes::deserialize_number_from_string;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
/// The setting collection.
|
||||
|
|
@ -16,6 +17,7 @@ pub struct Settings {
|
|||
/// * `port`: The port to listen on
|
||||
/// * `host`: The host address to listen on
|
||||
pub struct ApplicationSettings {
|
||||
#[serde(deserialize_with = "deserialize_number_from_string")]
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
}
|
||||
|
|
@ -31,6 +33,7 @@ pub struct ApplicationSettings {
|
|||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: Secret<String>,
|
||||
#[serde(deserialize_with = "deserialize_number_from_string")]
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub name: String,
|
||||
|
|
@ -88,6 +91,11 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
|||
.add_source(config::File::from(
|
||||
configuration_directory.join(environment_filename),
|
||||
))
|
||||
.add_source(
|
||||
config::Environment::with_prefix("APP")
|
||||
.prefix_separator("_")
|
||||
.separator("__"),
|
||||
)
|
||||
.build()?;
|
||||
|
||||
settings.try_deserialize::<Settings>()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue