- ssh: exec_timeout_secs 600s(408断会话)/idle 300→1800s/日志级别规则/截断500→2000 - mongo/redis: query_timeout 60s + idle_timeout 落配置 - 四代理: 日志默认开启(default_log_path exe同目录logs/),未设env也记录 - mysql: default_log_path 共享实现注释同步
119 lines
3.5 KiB
Rust
119 lines
3.5 KiB
Rust
use anyhow::{Result, bail};
|
|
use serde::Deserialize;
|
|
use std::fs;
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct ServerConfig {
|
|
pub port: u16,
|
|
#[serde(default = "default_host")]
|
|
pub host: String,
|
|
}
|
|
|
|
fn default_host() -> String { "127.0.0.1".to_string() }
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct PoolConfig {
|
|
#[serde(default = "default_idle_timeout")]
|
|
pub idle_timeout_secs: u64,
|
|
#[serde(default = "default_check_interval")]
|
|
pub check_interval_secs: u64,
|
|
/// 单操作执行超时(秒):防无界阻塞,超时返回 408
|
|
#[serde(default = "default_query_timeout")]
|
|
pub query_timeout_secs: u64,
|
|
}
|
|
|
|
fn default_idle_timeout() -> u64 { 300 }
|
|
fn default_check_interval() -> u64 { 60 }
|
|
fn default_query_timeout() -> u64 { 60 }
|
|
|
|
impl Default for PoolConfig {
|
|
fn default() -> Self {
|
|
Self { idle_timeout_secs: default_idle_timeout(), check_interval_secs: default_check_interval(), query_timeout_secs: default_query_timeout() }
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct ConnectionConfig {
|
|
pub name: String,
|
|
pub host: String,
|
|
#[serde(default = "default_port")]
|
|
pub port: u16,
|
|
#[serde(default)]
|
|
pub password: Option<String>,
|
|
#[serde(default)]
|
|
pub db: i64,
|
|
}
|
|
|
|
pub fn default_port() -> u16 { 6379 }
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Config {
|
|
#[serde(default = "default_server")]
|
|
pub server: ServerConfig,
|
|
#[serde(default)]
|
|
pub pool: PoolConfig,
|
|
pub connections: Vec<ConnectionConfig>,
|
|
}
|
|
|
|
fn default_server() -> ServerConfig {
|
|
ServerConfig { port: 3310, host: "127.0.0.1".to_string() }
|
|
}
|
|
|
|
impl Config {
|
|
pub fn from_file(path: &str) -> Result<Self> {
|
|
let content = fs::read_to_string(path)?;
|
|
let config: Config = toml::from_str(&content)?;
|
|
config.validate()?;
|
|
Ok(config)
|
|
}
|
|
|
|
fn validate(&self) -> Result<()> {
|
|
let mut names = std::collections::HashSet::new();
|
|
for conn in &self.connections {
|
|
if conn.name.is_empty() { bail!("Connection name cannot be empty"); }
|
|
if names.contains(&conn.name) { bail!("Duplicate connection name: {}", conn.name); }
|
|
names.insert(conn.name.clone());
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl ConnectionConfig {
|
|
pub fn build_url(&self) -> String {
|
|
match &self.password {
|
|
Some(pass) => format!("redis://:{}@{}:{}/{}", url_encode_password(pass), self.host, self.port, self.db),
|
|
None => format!("redis://{}:{}/{}", self.host, self.port, self.db),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn url_encode_password(s: &str) -> String {
|
|
let mut result = String::new();
|
|
for c in s.chars() {
|
|
match c {
|
|
'@' => result.push_str("%40"),
|
|
':' => result.push_str("%3A"),
|
|
'/' => result.push_str("%2F"),
|
|
'?' => result.push_str("%3F"),
|
|
'#' => result.push_str("%23"),
|
|
'[' => result.push_str("%5B"),
|
|
']' => result.push_str("%5D"),
|
|
'!' => result.push_str("%21"),
|
|
'$' => result.push_str("%24"),
|
|
'&' => result.push_str("%26"),
|
|
'\'' => result.push_str("%27"),
|
|
'(' => result.push_str("%28"),
|
|
')' => result.push_str("%29"),
|
|
'*' => result.push_str("%2A"),
|
|
'+' => result.push_str("%2B"),
|
|
',' => result.push_str("%2C"),
|
|
';' => result.push_str("%3B"),
|
|
'=' => result.push_str("%3D"),
|
|
'%' => result.push_str("%25"),
|
|
' ' => result.push_str("%20"),
|
|
_ => result.push(c),
|
|
}
|
|
}
|
|
result
|
|
}
|