diff options
author | rtkay123 <dev@kanjala.com> | 2025-08-12 21:05:07 +0200 |
---|---|---|
committer | rtkay123 <dev@kanjala.com> | 2025-08-12 21:05:07 +0200 |
commit | b6924a50c9ec49e1b2b0d286abbbe608410af87d (patch) | |
tree | 9c5cf583fedfbb585985ac829bbdfdadce1571fe /crates/router/src/state.rs | |
parent | d75b5fc9c0497f56e6b8602d8ff8991bfaeff18c (diff) | |
download | warden-b6924a50c9ec49e1b2b0d286abbbe608410af87d.tar.bz2 warden-b6924a50c9ec49e1b2b0d286abbbe608410af87d.zip |
feat(router): get config
Diffstat (limited to 'crates/router/src/state.rs')
-rw-r--r-- | crates/router/src/state.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/crates/router/src/state.rs b/crates/router/src/state.rs new file mode 100644 index 0000000..e01629e --- /dev/null +++ b/crates/router/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::routing::{ + RoutingConfiguration, query_routing_client::QueryRoutingClient, +}; +use warden_stack::{Configuration}; + +use crate::{ + cnfg::LocalConfig, + processor::grpc::interceptor::{Intercepted, MyInterceptor}, +}; + +#[derive(Clone)] +pub struct Services { + pub jetstream: Context, +} + +pub type AppHandle = Arc<AppState>; + +#[derive(Clone)] +pub struct AppState { + pub services: Services, + pub local_cache: Arc<RwLock<Cache<i32, RoutingConfiguration>>>, + pub config: LocalConfig, + pub query_routing_client: QueryRoutingClient<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_routing_client = QueryRoutingClient::with_interceptor(channel, MyInterceptor); + + Ok(Self { + services, + config, + local_cache: Arc::new(RwLock::new(Cache::builder().build())), + query_routing_client, + }) + } +} |