diff options
author | rtkay123 <dev@kanjala.com> | 2025-08-17 20:02:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-08-17 20:02:49 +0200 |
commit | 73d7bab8844bb21c7a9143c30800c2d11d411e42 (patch) | |
tree | 955290bd2bded56b534738d6320216fbeeb708cb /crates/typologies/src/state.rs | |
parent | 725739985d853b07d73fa7fcd6db1f2f1b0000b6 (diff) | |
download | warden-73d7bab8844bb21c7a9143c30800c2d11d411e42.tar.bz2 warden-73d7bab8844bb21c7a9143c30800c2d11d411e42.zip |
feat: typology processor (#8)
Diffstat (limited to 'crates/typologies/src/state.rs')
-rw-r--r-- | crates/typologies/src/state.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/crates/typologies/src/state.rs b/crates/typologies/src/state.rs new file mode 100644 index 0000000..23e08d9 --- /dev/null +++ b/crates/typologies/src/state.rs @@ -0,0 +1,55 @@ +use std::sync::Arc; + +use async_nats::jetstream::Context; +use moka::future::Cache; +use tokio::sync::RwLock; +use tonic::transport::Endpoint; +use tracing::error; +use warden_core::configuration::typology::{ + TypologyConfiguration, TypologyConfigurationRequest, + query_typologies_client::QueryTypologiesClient, +}; +use warden_stack::{Configuration, cache::RedisManager}; + +use crate::cnfg::LocalConfig; +use warden_middleware::grpc::interceptor::{Intercepted, MyInterceptor}; + +#[derive(Clone)] +pub struct Services { + pub jetstream: Context, + pub cache: RedisManager, +} + +pub type AppHandle = Arc<AppState>; + +#[derive(Clone)] +pub struct AppState { + pub services: Services, + pub local_cache: Arc<RwLock<Cache<TypologyConfigurationRequest, TypologyConfiguration>>>, + pub config: LocalConfig, + pub query_typology_client: QueryTypologiesClient<Intercepted>, +} + +impl AppState { + pub async fn new(services: Services, configuration: Configuration) -> anyhow::Result<Self> { + let config: LocalConfig = serde_json::from_value(configuration.misc.clone())?; + let channel = Endpoint::new(config.config_endpoint.to_string())? + .connect() + .await + .inspect_err(|e| { + error!( + endpoint = ?config.config_endpoint, + "could not connect to config service: {e}", + ) + })?; + + let query_typology_client = QueryTypologiesClient::with_interceptor(channel, MyInterceptor); + + Ok(Self { + services, + config, + local_cache: Arc::new(RwLock::new(Cache::builder().build())), + query_typology_client, + }) + } +} |