-
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.
feat: postgresql database configuration + update tests
- Loading branch information
Showing
8 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
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 @@ | ||
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" |
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
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: "newsletter" |
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 |
---|---|---|
@@ -1,2 +1,36 @@ | ||
#[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 get_connnection_string(&self) -> String { | ||
format!( | ||
"postgres://{}:{}@{}:{}/{}", | ||
self.username, self.password, self.host, self.port, self.database_name | ||
) | ||
} | ||
} | ||
|
||
pub fn get_configuration() -> Result<Settings, config::ConfigError> { | ||
// Initialize the configuration reader | ||
let settings = config::Config::builder() | ||
// Config file name | ||
.add_source(config::File::new( | ||
"configuration.yml", | ||
config::FileFormat::Yaml, | ||
)) | ||
.build()?; | ||
|
||
settings.try_deserialize::<Settings>() | ||
} |
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
use std::net::TcpListener; | ||
use zero2prod::startup::run; | ||
use zero2prod::{configurations::get_configuration, startup::run}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); | ||
// We want to panic if we cannot read the configuration | ||
let configuration = get_configuration().expect("Failed to read configurations"); | ||
// Bind the TCP listener socket address with the configuration port | ||
let address = format!("127.0.0.1:{}", configuration.application_port); | ||
let listener = TcpListener::bind(address).expect("Failed to bind random port"); | ||
run(listener)?.await | ||
} |
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