diff --git a/example.env b/example.env index 90842b3..c6ad823 100644 --- a/example.env +++ b/example.env @@ -6,6 +6,8 @@ AUTH.REDIRECT_URL=http://localhost:3000/auth/callback AUTH.AUTH_URL=https://example.com/authorize AUTH.TOKEN_URL=https://example.com/token AUTH.USERINFO_URL=https://example.com/userinfo +# The .env parser (dotenvy) requires quotes around any value with spaces. Note +# that in this regard it is incompatible with Docker's --env-file parser. EMAIL.VERIFICATION_FROM=no-reply@shout.dev EMAIL.MESSAGE_FROM=no-reply@shout.dev EMAIL.SMTP.SERVER=smtp.example.com diff --git a/src/main.rs b/src/main.rs index 5a957ee..6e1bc4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,12 +57,12 @@ enum Commands { #[tokio::main] async fn main() { - let settings = Settings::load().unwrap(); - tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .init(); + let settings = Settings::load().unwrap(); + let cli = Cli::parse(); let database_url = settings.database_url.clone(); diff --git a/src/settings.rs b/src/settings.rs index 0418216..45e6ae0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,3 +1,4 @@ +use anyhow::{Context as _, Result}; use axum::extract::FromRef; use config::{Config, ConfigError, Environment}; use dotenvy::dotenv; @@ -78,14 +79,29 @@ pub struct SlackSettings { } impl Settings { - pub fn load() -> Result { - if let Err(err) = dotenv() { - tracing::warn!("Couldn't load .env file: {:?}", err); + pub fn load() -> Result { + match dotenv() { + Err(err) => { + if err.not_found() { + tracing::info!("no .env file found"); + } else { + return Err(err).context("dotenvy error"); + } + } + Ok(pathbuf) => { + tracing::info!( + "using env file {}", + pathbuf + .to_str() + .ok_or(anyhow::anyhow!("pathbuf is not valid unicode"))? + ); + } } let s = Config::builder() .add_source(Environment::default()) - .build()?; - s.try_deserialize() + .build() + .context("config error")?; + Ok(s.try_deserialize().context("deserialize error")?) } }