mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
feat: add remote config over http/redis
This commit is contained in:
parent
c4db95796d
commit
a72afb35a0
12 changed files with 326 additions and 4 deletions
|
|
@ -8,6 +8,7 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilte
|
|||
use sentry_tracing::event_from_event;
|
||||
|
||||
pub mod db;
|
||||
pub mod runtime_config;
|
||||
pub mod state;
|
||||
|
||||
pub mod _config;
|
||||
|
|
|
|||
72
crates/libpk/src/runtime_config.rs
Normal file
72
crates/libpk/src/runtime_config.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use fred::{clients::RedisPool, interfaces::HashesInterface};
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::RwLock;
|
||||
use tracing::info;
|
||||
|
||||
pub struct RuntimeConfig {
|
||||
redis: RedisPool,
|
||||
settings: RwLock<HashMap<String, String>>,
|
||||
redis_key: String,
|
||||
}
|
||||
|
||||
impl RuntimeConfig {
|
||||
pub async fn new(redis: RedisPool, component_key: String) -> anyhow::Result<Self> {
|
||||
let redis_key = format!("remote_config:{component_key}");
|
||||
|
||||
let mut c = RuntimeConfig {
|
||||
redis,
|
||||
settings: RwLock::new(HashMap::new()),
|
||||
redis_key,
|
||||
};
|
||||
|
||||
c.load().await?;
|
||||
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
pub async fn load(&mut self) -> anyhow::Result<()> {
|
||||
let redis_config: HashMap<String, String> = self.redis.hgetall(&self.redis_key).await?;
|
||||
|
||||
let mut settings = self.settings.write().await;
|
||||
|
||||
for (key, value) in redis_config {
|
||||
settings.insert(key, value);
|
||||
}
|
||||
|
||||
info!("starting with runtime config: {:?}", self.settings);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set(&self, key: String, value: String) -> anyhow::Result<()> {
|
||||
self.redis
|
||||
.hset::<(), &str, (String, String)>(&self.redis_key, (key.clone(), value.clone()))
|
||||
.await?;
|
||||
self.settings
|
||||
.write()
|
||||
.await
|
||||
.insert(key.clone(), value.clone());
|
||||
info!("updated runtime config: {key}={value}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete(&self, key: String) -> anyhow::Result<()> {
|
||||
self.redis
|
||||
.hdel::<(), &str, String>(&self.redis_key, key.clone())
|
||||
.await?;
|
||||
self.settings.write().await.remove(&key.clone());
|
||||
info!("updated runtime config: {key} removed");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get(&self, key: String) -> Option<String> {
|
||||
self.settings.read().await.get(&key).cloned()
|
||||
}
|
||||
|
||||
pub async fn exists(&self, key: &str) -> bool {
|
||||
self.settings.read().await.contains_key(key)
|
||||
}
|
||||
|
||||
pub async fn get_all(&self) -> HashMap<String, String> {
|
||||
self.settings.read().await.clone()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue