This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use configuration to connect to db (#11)
* Add configuration yaml file * Add config dependency * Add configuration model and postgres connect string method * Use configuration to connect to db
- Loading branch information
1 parent
51f270a
commit bfd1a2b
Showing
6 changed files
with
226 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
application_port: 8000 | ||
database: | ||
host: "127.0.0.1" | ||
port: 5432 | ||
username: "postgres" | ||
password: "password" | ||
database_name: "briefly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use config::Config; | ||
|
||
#[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, | ||
} | ||
|
||
pub fn get_configuration() -> Result<Settings, config::ConfigError> { | ||
let builder = Config::builder().add_source(config::File::with_name("configuration")); | ||
let config = builder.build()?; | ||
config.try_deserialize() | ||
} | ||
|
||
impl DatabaseSettings { | ||
pub fn connection_string(&self) -> String { | ||
format!( | ||
"postgres://{}:{}@{}:{}/{}", | ||
self.username, self.password, self.host, self.port, self.database_name | ||
) | ||
} | ||
} |
Oops, something went wrong.