From a0e0713a1a7005d8ad953423b18ffe7c9e3dc903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=B4=AB=E8=B6=85?= <5364766+auditoreyzc@user.noreply.gitee.com> Date: Thu, 16 Jan 2025 12:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96uuid=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E9=80=89=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 1 + src/cores/mod.rs | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bdf8b1e..336270b 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 1690bea..3767b31 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(()) } - -- Gitee