magnetar/src/service/mod.rs

56 lines
1.6 KiB
Rust

use magnetar_calckey_model::{CalckeyCache, CalckeyModel};
use magnetar_common::config::MagnetarConfig;
use std::fmt::{Debug, Formatter};
use thiserror::Error;
pub mod emoji_cache;
pub mod instance_meta_cache;
pub mod user_cache;
pub struct MagnetarService {
pub db: CalckeyModel,
pub cache: CalckeyCache,
pub config: &'static MagnetarConfig,
pub auth_cache: user_cache::UserCacheService,
pub instance_meta_cache: instance_meta_cache::InstanceMetaCacheService,
pub emoji_cache: emoji_cache::EmojiCacheService,
}
impl Debug for MagnetarService {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MagnetarService")
.field("db", &self.db)
.field("cache", &self.cache)
.field("config", &self.config)
.finish_non_exhaustive()
}
}
#[derive(Debug, Error)]
pub enum ServiceInitError {
#[error("Authentication cache initialization error: {0}")]
AuthCacheError(#[from] user_cache::UserCacheError),
}
impl MagnetarService {
pub async fn new(
config: &'static MagnetarConfig,
db: CalckeyModel,
cache: CalckeyCache,
) -> Result<Self, ServiceInitError> {
let auth_cache =
user_cache::UserCacheService::new(config, db.clone(), cache.clone()).await?;
let instance_meta_cache = instance_meta_cache::InstanceMetaCacheService::new(db.clone());
let emoji_cache = emoji_cache::EmojiCacheService::new(db.clone());
Ok(Self {
db,
cache,
config,
auth_cache,
instance_meta_cache,
emoji_cache,
})
}
}