feat: use configuration.yaml to setup variables

This commit is contained in:
2024-05-03 16:14:36 -04:00
parent f08def8e77
commit 38f9646526
5 changed files with 237 additions and 13 deletions

View File

@@ -0,0 +1,25 @@
#[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 settings = config::Config::builder()
.add_source(config::File::new(
"configuration.yaml",
config::FileFormat::Yaml,
))
.build()?;
settings.try_deserialize::<Settings>()
}

View File

@@ -1,10 +1,15 @@
use std::net::TcpListener;
use email_newsletter_api::startup;
use email_newsletter_api::{configuration::get_configuration, startup};
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000");
let configuration = get_configuration().expect("Failed to read configuration");
let port_number = configuration.application_port;
let listener = TcpListener::bind(format!("127.0.0.1:{}", port_number))
.unwrap_or_else(|_| panic!("Can't bind to port {} at localhost", port_number));
// Move the error up the call stack
// otherwise await for the HttpServer