1
0
Fork 0
forked from 2sys/shoutdotdev

improve logging around config loading

This commit is contained in:
Brent Schroeter 2025-03-11 10:29:22 -07:00
parent c7fc56cff3
commit e58e5002a0
3 changed files with 25 additions and 7 deletions

View file

@ -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

View file

@ -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();

View file

@ -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<Self, ConfigError> {
if let Err(err) = dotenv() {
tracing::warn!("Couldn't load .env file: {:?}", err);
pub fn load() -> Result<Self> {
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")?)
}
}