buzzrelay/src/state.rs

41 lines
1.2 KiB
Rust
Raw Permalink Normal View History

2023-10-12 17:04:05 +00:00
use axum::{
extract::FromRef,
};
use sigh::{PrivateKey, PublicKey};
use std::sync::Arc;
use crate::{config::Config, db::Database, actor_cache::ActorCache};
2023-10-12 17:04:05 +00:00
#[derive(Clone)]
pub struct State {
pub database: Database,
2023-10-12 20:09:09 +00:00
pub redis: Option<(redis::aio::ConnectionManager, Arc<String>)>,
2023-10-12 17:04:05 +00:00
pub client: Arc<reqwest::Client>,
pub actor_cache: ActorCache,
2023-10-12 17:04:05 +00:00
pub hostname: Arc<String>,
pub priv_key: Arc<PrivateKey>,
pub pub_key: Arc<PublicKey>,
}
impl FromRef<State> for Arc<reqwest::Client> {
fn from_ref(state: &State) -> Arc<reqwest::Client> {
state.client.clone()
}
}
impl State {
2023-10-12 20:09:09 +00:00
pub fn new(config: Config, database: Database, redis: Option<(redis::aio::ConnectionManager, String)>, client: reqwest::Client) -> Self {
2023-10-12 17:04:05 +00:00
let priv_key = Arc::new(config.priv_key());
let pub_key = Arc::new(config.pub_key());
State {
database,
2023-10-12 20:09:09 +00:00
redis: redis.map(|(connection, in_topic)| (connection, Arc::new(in_topic))),
2023-10-12 17:04:05 +00:00
client: Arc::new(client),
2024-03-27 20:39:40 +00:00
actor_cache: ActorCache::default(),
2023-10-12 17:04:05 +00:00
hostname: Arc::new(config.hostname),
priv_key,
pub_key,
}
}
}