Skip to content
Clone
b8f89bc
58 lines 1.6 KB Rust
pub mod database;
pub mod error;
pub mod health;
pub mod middleware;
pub mod pagination;
pub mod redis;
pub mod rest;
pub mod storage;
use std::sync::Arc;
use config::AppConfig;
pub use database::{AppDatabase, DatabaseRole};
pub use error::ExportError;
pub use redis::{AppRedis, RedisConnection};
pub use storage::AppStorage;
pub use email::EmailService;
#[derive(Clone)]
pub struct AppState {
pub database: Arc<AppDatabase>,
pub redis: AppRedis,
pub storage: Arc<AppStorage>,
pub email: EmailService,
pub config: AppConfig,
}
impl AppState {
pub async fn load() -> Result<Self, ExportError> {
let config =
config::AppConfig::load().map_err(|e| ExportError::InvalidConfig(e.to_string()))?;
Self::with_config(config).await
}
pub async fn with_config(config: AppConfig) -> Result<Self, ExportError> {
config
.validate()
.map_err(|e| ExportError::InvalidConfig(e.to_string()))?;
let database = Arc::new(AppDatabase::connect(config.database.clone()).await?);
let redis = AppRedis::connect(config.redis.clone()).await?;
let storage = Arc::new(AppStorage::connect(config.s3.clone())?);
let redis_url = redis.config().url().to_string();
let builder = email::EmailServiceBuilder::new(config.email.clone())
.with_queue(redis_url, config.email_queue.clone())
.register_default_templates()
.await;
let email = builder.build().await?;
Ok(Self {
database,
redis,
storage,
email,
config,
})
}
}