forked from Eeveid/lightOps
实现 LightOps 运维面板基础功能
This commit is contained in:
59
crates/lightops-agent/src/config.rs
Normal file
59
crates/lightops-agent/src/config.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs, path::Path};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AgentConfig {
|
||||
pub server_url: String,
|
||||
pub agent_id: Option<String>,
|
||||
pub token: Option<String>,
|
||||
pub secret: Option<String>,
|
||||
pub name: Option<String>,
|
||||
pub heartbeat_interval: Option<u64>,
|
||||
#[serde(skip)]
|
||||
pub config_path: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for AgentConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
server_url: "http://127.0.0.1:8080".to_string(),
|
||||
agent_id: None,
|
||||
token: None,
|
||||
secret: None,
|
||||
name: None,
|
||||
heartbeat_interval: Some(30),
|
||||
config_path: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AgentConfig {
|
||||
pub fn load_optional(path: &str) -> Result<Self> {
|
||||
if Path::new(path).exists() {
|
||||
Ok(toml::from_str(&fs::read_to_string(path)?)?)
|
||||
} else {
|
||||
Ok(Self::default())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, path: &str) -> Result<()> {
|
||||
if let Some(parent) = Path::new(path).parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
fs::write(path, toml::to_string_pretty(self)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ws_url(&self) -> Result<String> {
|
||||
let mut url = Url::parse(&self.server_url)?;
|
||||
url.set_scheme(match url.scheme() {
|
||||
"https" => "wss",
|
||||
_ => "ws",
|
||||
})
|
||||
.ok();
|
||||
url.set_path("/api/agent/ws");
|
||||
Ok(url.to_string())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user