-
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.
- Loading branch information
1 parent
656b261
commit 201c412
Showing
5 changed files
with
61 additions
and
4 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 |
---|---|---|
|
@@ -16,4 +16,7 @@ Cargo.toml.orig | |
|
||
# Ignore OS-specific files | ||
.DS_Store | ||
Thumbs.db | ||
Thumbs.db | ||
|
||
.env | ||
configuration.yaml |
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,34 @@ | ||
#[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> { | ||
// Initialise our configuration reader | ||
let mut settings = config::Config::default(); | ||
|
||
// Add configuration values from a file name `configuration` | ||
settings.merge(config::File::with_name("configuration"))?; | ||
|
||
// Try to convert the configuration values it read into out Settings type | ||
settings.try_into() | ||
} | ||
|
||
impl DatabaseSettings { | ||
pub fn connection_string(&self) -> String { | ||
format!( | ||
"postgres://{}:{}@{}:{}/{}", | ||
self.username, self.password, self.host, self.port, self.database_name | ||
) | ||
} | ||
} |
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,9 +1,12 @@ | ||
use std::net::TcpListener; | ||
use newsletter::startup::run; | ||
use newsletter::configuration::get_configuration; | ||
|
||
#[tokio::main] | ||
async fn main() -> std::io::Result<()> { | ||
let listener = TcpListener::bind("127.0.0.1:0") | ||
.expect("Failed to bind random port"); | ||
// Panic if we can't read configuration | ||
let configuration = get_configuration().expect("Failed to read configuration."); | ||
let address = format!("127.0.0.1:{}", configuration.application_port); | ||
let listener = TcpListener::bind(address)?; | ||
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