refactor: adapters configured from config crate

This commit is contained in:
2026-10-25 21:07:19 +01:00
parent 1f724bb064
commit 04f9c13625
45 changed files with 277 additions and 177 deletions

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition.workspace = true
[dependencies]
config = { workspace = true }
async-trait = { workspace = true }
domain = { workspace = true }
anyhow = { workspace = true }

View File

@@ -22,19 +22,17 @@ pub struct AuthConfig {
}
impl AuthConfig {
pub fn from_env() -> anyhow::Result<Self> {
let secret = std::env::var("JWT_SECRET")
.map_err(|_| anyhow::anyhow!("JWT_SECRET env var is required"))?;
pub fn from_config(config: &config::AuthConfig) -> anyhow::Result<Self> {
let secret = config
.jwt_secret
.clone()
.ok_or_else(|| anyhow::anyhow!("JWT_SECRET env var is required"))?;
if secret.is_empty() {
anyhow::bail!("JWT_SECRET must not be empty");
}
let ttl_seconds = std::env::var("JWT_TTL_SECONDS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(900u64);
Ok(Self {
secret,
ttl_seconds,
ttl_seconds: config.jwt_ttl_seconds,
})
}
}
@@ -107,11 +105,13 @@ impl PasswordHasher for Argon2PasswordHasher {
}
}
pub fn create() -> anyhow::Result<(
pub fn create(
config: &config::AuthConfig,
) -> anyhow::Result<(
std::sync::Arc<dyn domain::ports::AuthService>,
std::sync::Arc<dyn domain::ports::PasswordHasher>,
)> {
let config = AuthConfig::from_env()?;
let config = AuthConfig::from_config(config)?;
Ok((
std::sync::Arc::new(JwtAuthService::new(config)),
std::sync::Arc::new(Argon2PasswordHasher),