Skip to main content

flare_core_base/config/
mod.rs

1use serde::{Deserialize, Serialize};
2
3mod layered;
4pub use layered::LayeredConfig;
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct Config {
8    pub service: ServiceConfig,
9    pub server: ServerConfig,
10    pub registry: Option<RegistryConfig>,
11    pub mesh: Option<MeshConfig>,
12    pub storage: Option<StorageConfig>,
13}
14
15#[derive(Debug, Clone, Deserialize, Serialize)]
16pub struct StorageConfig {
17    pub storage_type: String, // minio, s3, tencent, alibaba
18    pub endpoint: Option<String>,
19    pub access_key: Option<String>,
20    pub secret_key: Option<String>,
21    pub bucket: Option<String>,
22    pub region: Option<String>,
23    pub use_ssl: Option<bool>,
24}
25
26#[derive(Debug, Clone, Deserialize, Serialize)]
27pub struct ServiceConfig {
28    pub name: String,
29    pub version: String,
30}
31
32#[derive(Debug, Clone, Deserialize, Serialize)]
33pub struct ServerConfig {
34    pub address: String,
35    pub port: u16,
36}
37
38#[derive(Debug, Clone, Deserialize, Serialize)]
39pub struct RegistryConfig {
40    #[serde(default = "default_registry_type")]
41    pub registry_type: String, // etcd, consul, mesh
42    pub endpoints: Vec<String>,
43    pub namespace: String,
44    pub ttl: u64,
45    /// 负载均衡策略:round_robin, random, consistent_hash, least_connections
46    /// 默认:consistent_hash
47    #[serde(default = "default_load_balance_strategy")]
48    pub load_balance_strategy: String,
49}
50
51fn default_load_balance_strategy() -> String {
52    "consistent_hash".to_string()
53}
54
55fn default_registry_type() -> String {
56    "etcd".to_string()
57}
58
59#[derive(Debug, Clone, Deserialize, Serialize)]
60pub struct MeshConfig {
61    pub enabled: bool,
62    pub service_name: String,
63    pub namespace: String,
64}
65
66impl Config {
67    pub fn load_from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
68        let content = std::fs::read_to_string(path)?;
69        let config: Config = toml::from_str(&content)?;
70        Ok(config)
71    }
72}