use std::sync::Arc; use sqlx::PgPool; use stack_up::{ Configuration, cache::{RedisConnection, RedisManager}, }; use tonic::Status; use tracing::error; use crate::cnfg::LocalConfig; #[derive(Clone)] pub struct AppHandle(Arc); impl std::ops::Deref for AppHandle { type Target = Arc; fn deref(&self) -> &Self::Target { &self.0 } } #[derive(Clone)] pub struct Services { pub postgres: PgPool, pub cache: RedisManager, } impl Services { pub fn new(postgres: PgPool, cache: RedisManager) -> Self { Self { postgres, cache } } } pub struct AppState { pub services: Services, pub local_config: LocalConfig, } impl AppState { pub async fn create( services: Services, configuration: &Configuration, ) -> Result { let local_config: LocalConfig = serde_json::from_value(configuration.misc.clone())?; Ok(AppHandle(Arc::new(Self { services, local_config, }))) } pub async fn cache(&self) -> Result { let cache = self .services .cache .get() .await .inspect_err(|e| error!("{e}")) .map_err(|_e| Status::internal("storage not ready"))?; Ok(cache) } }