mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-06-17 04:30:38 +00:00
39 lines
946 B
Rust
39 lines
946 B
Rust
|
#[derive(serde::Deserialize)]
|
||
|
pub struct Settings {
|
||
|
pub database: DatabaseSettings,
|
||
|
pub application_port: u16,
|
||
|
}
|
||
|
|
||
|
#[derive(serde::Deserialize)]
|
||
|
pub struct DatabaseSettings {
|
||
|
pub username: String,
|
||
|
pub password: String,
|
||
|
pub port: u16,
|
||
|
pub host: String,
|
||
|
pub database_name: String,
|
||
|
}
|
||
|
|
||
|
impl DatabaseSettings {
|
||
|
pub fn connection_string(&self) -> String {
|
||
|
format!(
|
||
|
"postgres://{}:{}@{}:{}/{}",
|
||
|
self.username, self.password, self.host, self.port, self.database_name
|
||
|
)
|
||
|
}
|
||
|
|
||
|
pub fn connection_string_without_db(&self) -> String {
|
||
|
format!(
|
||
|
"postgres://{}:{}@{}:{}",
|
||
|
self.username, self.password, self.host, self.port
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||
|
let mut settings = config::Config::default();
|
||
|
|
||
|
settings.merge(config::File::with_name("configuration"))?;
|
||
|
|
||
|
settings.try_into()
|
||
|
}
|