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 = "2021"
[dependencies]
config = { workspace = true }
sqlx = { workspace = true }
anyhow = { workspace = true }

View File

@@ -4,6 +4,8 @@ pub struct AppConfig {
pub base_url: String,
pub rate_limit: u64,
pub refresh_ttl_seconds: u64,
pub secure_cookies: bool,
pub cors_origins: String,
pub wrapup: WrapUpConfig,
}
@@ -14,39 +16,26 @@ pub struct WrapUpConfig {
pub bg_dir: Option<String>,
}
impl AppConfig {
pub fn from_env() -> Self {
let allow_registration = std::env::var("ALLOW_REGISTRATION")
.map(|v| v == "true" || v == "1")
.unwrap_or(false);
let base_url = std::env::var("BASE_URL")
.unwrap_or_else(|_| "http://localhost:3000".to_string())
.trim_end_matches('/')
.to_string();
let rate_limit = std::env::var("RATE_LIMIT")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(60);
let refresh_ttl_seconds = std::env::var("REFRESH_TTL_SECONDS")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(2_592_000u64);
impl From<&::config::AppConfig> for AppConfig {
fn from(config: &::config::AppConfig) -> Self {
Self {
allow_registration,
base_url,
rate_limit,
refresh_ttl_seconds,
wrapup: WrapUpConfig::from_env(),
allow_registration: config.server.allow_registration,
base_url: config.server.base_url.clone(),
rate_limit: config.server.rate_limit,
refresh_ttl_seconds: config.auth.refresh_ttl_seconds,
secure_cookies: config.server.secure_cookies,
cors_origins: config.server.cors_origins.clone(),
wrapup: WrapUpConfig::from(&config.wrapup),
}
}
}
impl WrapUpConfig {
pub fn from_env() -> Self {
impl From<&::config::WrapUpConfig> for WrapUpConfig {
fn from(config: &::config::WrapUpConfig) -> Self {
Self {
font_path: std::env::var("WRAPUP_FONT_PATH").ok(),
logo_path: std::env::var("WRAPUP_LOGO_PATH").ok(),
bg_dir: std::env::var("WRAPUP_BG_DIR").ok(),
font_path: config.font_path.clone(),
logo_path: config.logo_path.clone(),
bg_dir: config.bg_dir.clone(),
}
}
}

View File

@@ -14,11 +14,8 @@ pub enum EventBusBackend {
}
impl EventBusBackend {
pub fn from_env() -> anyhow::Result<Self> {
match std::env::var("EVENT_BUS_BACKEND")
.unwrap_or_else(|_| "db".to_string())
.as_str()
{
pub fn from_config(config: &::config::EventsConfig) -> anyhow::Result<Self> {
match config.bus_backend.as_str() {
"db" => Ok(Self::Db),
#[cfg(feature = "nats")]
"nats" => Ok(Self::Nats),