diff --git a/Cargo.toml b/Cargo.toml index bdf8b1ed05e85438300e88759abfe7883f0a24d5..336270b80ff1410f0203101164bfea8565910db4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,3 +61,4 @@ strum = { version = "0.26.3", features = ["derive"] } serde_yaml = "0.9.34+deprecated" futures = "0.3.31" uuid = { version = "1.11.1", features = ["v4"] } +dirs = "6.0.0" diff --git a/src/cores/mod.rs b/src/cores/mod.rs index 1690bea0ec71c9d51cfb6dad62aaeb7dc77b3747..3767b31bfe483e8721f90aef03534fb385e56d0d 100644 --- a/src/cores/mod.rs +++ b/src/cores/mod.rs @@ -4,28 +4,28 @@ * since: 0.1.0 * **/ - use crate::cores::apiserver::{APIServer, AppState}; -use crate::cores::events::{P2PEventServer}; +use crate::cores::events::P2PEventServer; use crate::cores::handlers::DefaultHandler; +use crate::cores::plugin::PluginManager; use crate::db::db::DbPool; +use client_rust::event_client::WatchEventPublisher; use feventbus::impls::nats::nats::NatsCli; use feventbus::traits::controller::EventBus; use serde::{Deserialize, Serialize}; use std::sync::Arc; -use client_rust::event_client::WatchEventPublisher; -use crate::cores::plugin::PluginManager; + pub mod apiserver; -pub mod handlers; pub mod checker; pub mod events; -pub mod services; +pub mod handlers; pub mod plugin; -use tokio::sync::Mutex; -use uuid::Uuid; +pub mod services; use std::fs; use std::io::{self, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; +use tokio::sync::Mutex; +use uuid::Uuid; lazy_static::lazy_static! { pub static ref GLOBAL_SQ_LOCK: Arc> = Arc::new(Mutex::new(())); @@ -38,7 +38,7 @@ pub struct ResourcesMessage { plural: String, } -fn get_or_create_uuid(file_path: &str) -> io::Result { +fn get_or_create_uuid(file_path: &PathBuf) -> io::Result { let path = Path::new(file_path); if path.exists() { @@ -63,15 +63,17 @@ pub async fn prepare_app_state(database_url: &str) -> anyhow::Result { let handler: DefaultHandler = DefaultHandler::new(); let nats_cli = Arc::new(NatsCli::new().await?); let watch_event_publisher = Arc::new(WatchEventPublisher::new(nats_cli.clone())); - let file_path = "/root/iscas/fleet/uuid.conf"; - let cluster_id = get_or_create_uuid(file_path)?; + // let file_path = "/root/iscas/fleet/uuid.conf"; + let config_dir = dirs::config_dir().expect("config dir not found"); + let file_path = config_dir.join("iscas").join("fleet").join("uuid.conf"); + let cluster_id = get_or_create_uuid(&file_path)?; Ok(AppState { db_pool, handler: Arc::new(handler), nats_cli, watch_event_publisher, - cluster_id + cluster_id, }) } @@ -83,17 +85,24 @@ pub async fn prepare_app_state(database_url: &str) -> anyhow::Result { /// /// # 返回值 /// - 异步运行结果 -pub async fn start_server(database_url: &str, address: &str, plugin_manager: Arc) -> anyhow::Result<()> { +pub async fn start_server( + database_url: &str, + address: &str, + plugin_manager: Arc, +) -> anyhow::Result<()> { let app_state = prepare_app_state(database_url).await?; // 启动watch相关事件监听协程 app_state.clone().watch_event_publisher.start(); // 启动P2P事件监听协程 - P2PEventServer::new(Arc::from(app_state.clone())).start().await; + P2PEventServer::new(Arc::from(app_state.clone())) + .start() + .await; // 从插件管理器加载路由 let custom_route_providers = Arc::new(plugin_manager.get_providers()); - APIServer::new().start(address, app_state, custom_route_providers).await?; + APIServer::new() + .start(address, app_state, custom_route_providers) + .await?; Ok(()) } -