Skip to content
Clone
2cb5052
72 lines 2.1 KB Rust
pub mod error;
pub mod health;
use std::sync::Arc;
use config::AppConfig;
pub use error::BootstrapError;
#[derive(Clone)]
pub struct AppState {
pub database: Arc<db::AppDatabase>,
pub redis: cache::AppRedis,
pub cache: cache::Cache,
pub storage: Arc<storage::S3Client>,
pub config: AppConfig,
pub health: health::HealthCache,
pub mail: Option<email::MailPublisher>,
pub events: Option<events::EventHandler>,
}
impl AppState {
pub async fn load() -> Result<Self, BootstrapError> {
let config =
config::AppConfig::load().map_err(|e| BootstrapError::InvalidConfig(e.to_string()))?;
Self::with_config(config).await
}
pub async fn with_config(config: AppConfig) -> Result<Self, BootstrapError> {
let database: Arc<db::AppDatabase> =
Arc::new(db::AppDatabase::connect(config.database.clone()).await?);
Self::with_identity(config, database, None, None).await
}
pub async fn with_identity(
config: AppConfig,
database: Arc<db::AppDatabase>,
mail: Option<email::MailPublisher>,
events: Option<events::EventHandler>,
) -> Result<Self, BootstrapError> {
let conn = cache::CacheConn::init(&config.redis).await?;
let redis = cache::AppRedis::from_conn(conn.clone());
let cache = cache::Cache::from_conn(conn, &config.redis);
let storage: Arc<storage::S3Client> = Arc::new(storage::S3Client::new(&config.s3)?);
Ok(Self::from_parts(
database, redis, cache, storage, config, mail, events,
))
}
#[allow(clippy::too_many_arguments)]
pub fn from_parts(
database: Arc<db::AppDatabase>,
redis: cache::AppRedis,
cache: cache::Cache,
storage: Arc<storage::S3Client>,
config: AppConfig,
mail: Option<email::MailPublisher>,
events: Option<events::EventHandler>,
) -> Self {
Self {
database,
redis,
cache,
storage,
config,
health: health::HealthCache::default(),
mail,
events,
}
}
}