feat: 初步增加 Windows 支持

This commit is contained in:
mofeng-git
2026-05-18 22:43:28 +08:00
parent 0b9d94f53f
commit 935fa823f2
163 changed files with 11419 additions and 7581 deletions

View File

@@ -4,29 +4,20 @@ use std::sync::Arc;
use tokio::sync::broadcast;
use tokio::sync::Mutex;
use super::persistence::ConfigChange;
use super::AppConfig;
use super::ConfigChange;
use crate::error::{AppError, Result};
/// Configuration store backed by SQLite
///
/// Uses `ArcSwap` for lock-free reads, providing high performance
/// for frequent configuration access in hot paths.
#[derive(Clone)]
pub struct ConfigStore {
pool: Pool<Sqlite>,
/// Lock-free cache using ArcSwap for zero-cost reads
cache: Arc<ArcSwap<AppConfig>>,
change_tx: broadcast::Sender<ConfigChange>,
/// Serializes `set` / `update` so concurrent PATCH handlers cannot clobber each other
write_lock: Arc<Mutex<()>>,
}
impl ConfigStore {
/// Create a new configuration store
pub fn new(pool: Pool<Sqlite>) -> Result<Self> {
// Load or create default config synchronously wrapper
// (actual DB load is async, handled in init())
Ok(Self {
pool,
cache: Arc::new(ArcSwap::from_pointee(AppConfig::default())),
@@ -35,14 +26,12 @@ impl ConfigStore {
})
}
/// Load configuration from database (call after new())
pub async fn load(&self) -> Result<()> {
let config = Self::load_config(&self.pool).await?;
self.cache.store(Arc::new(config));
Ok(())
}
/// Load configuration from database
async fn load_config(pool: &Pool<Sqlite>) -> Result<AppConfig> {
let row: Option<(String,)> =
sqlx::query_as("SELECT value FROM config WHERE key = 'app_config'")
@@ -54,7 +43,6 @@ impl ConfigStore {
serde_json::from_str(&json).map_err(|e| AppError::Config(e.to_string()))
}
None => {
// Create default config
let config = AppConfig::default();
Self::save_config_to_db(pool, &config).await?;
Ok(config)
@@ -62,7 +50,6 @@ impl ConfigStore {
}
}
/// Save configuration to database
async fn save_config_to_db(pool: &Pool<Sqlite>, config: &AppConfig) -> Result<()> {
let json = serde_json::to_string(config)?;
@@ -80,21 +67,15 @@ impl ConfigStore {
Ok(())
}
/// Get current configuration (lock-free, zero-copy)
///
/// Returns an `Arc<AppConfig>` for efficient sharing without cloning.
/// This is a lock-free operation with minimal overhead.
pub fn get(&self) -> Arc<AppConfig> {
self.cache.load_full()
}
/// Set entire configuration
pub async fn set(&self, config: AppConfig) -> Result<()> {
let _guard = self.write_lock.lock().await;
Self::save_config_to_db(&self.pool, &config).await?;
self.cache.store(Arc::new(config));
// Notify subscribers
let _ = self.change_tx.send(ConfigChange {
key: "app_config".to_string(),
});
@@ -102,27 +83,19 @@ impl ConfigStore {
Ok(())
}
/// Update configuration with a closure
///
/// Uses read-modify-write under a mutex so concurrent `update` / `set` calls are serialized
/// and merged correctly (each closure sees the latest stored config).
pub async fn update<F>(&self, f: F) -> Result<()>
where
F: FnOnce(&mut AppConfig),
{
let _guard = self.write_lock.lock().await;
// Load current config, clone it for modification
let current = self.cache.load();
let mut config = (**current).clone();
f(&mut config);
// Persist to database first
Self::save_config_to_db(&self.pool, &config).await?;
// Then update cache atomically
self.cache.store(Arc::new(config));
// Notify subscribers
let _ = self.change_tx.send(ConfigChange {
key: "app_config".to_string(),
});
@@ -130,12 +103,10 @@ impl ConfigStore {
Ok(())
}
/// Subscribe to configuration changes
pub fn subscribe(&self) -> broadcast::Receiver<ConfigChange> {
self.change_tx.subscribe()
}
/// Check if system is initialized (lock-free)
pub fn is_initialized(&self) -> bool {
self.cache.load().initialized
}
@@ -158,11 +129,9 @@ mod tests {
let store = ConfigStore::new(db.clone_pool()).unwrap();
store.load().await.unwrap();
// Check default config (now lock-free, no await needed)
let config = store.get();
assert!(!config.initialized);
// Update config
store
.update(|c| {
c.initialized = true;
@@ -171,12 +140,10 @@ mod tests {
.await
.unwrap();
// Verify update
let config = store.get();
assert!(config.initialized);
assert_eq!(config.web.http_port, 9000);
// Create new store instance and verify persistence
let store2 = ConfigStore::new(db.clone_pool()).unwrap();
store2.load().await.unwrap();
let config = store2.get();