Skip to main content

conreg_client/
conf.rs

1/// Configuration component
2use crate::utils;
3use derive_builder::Builder;
4use serde::Deserialize;
5use serde_yaml::Value;
6use std::collections::HashMap;
7
8/// Overall configuration for config/registry center
9/// Wrapped because the top-level key in bootstrap.yaml is conreg
10#[derive(Debug, Clone, Deserialize)]
11pub(crate) struct ConRegConfigWrapper {
12    pub(crate) conreg: ConRegConfig,
13}
14
15/// Config/Registry Center Configuration
16#[derive(Debug, Clone, Deserialize, Builder)]
17#[serde(rename_all = "kebab-case")]
18pub struct ConRegConfig {
19    /// Service ID
20    #[allow(unused)]
21    #[serde(default = "ConRegConfig::default_service_id")]
22    #[builder(setter(into), default = "ConRegConfig::default_service_id()")]
23    pub service_id: String,
24    /// Client configuration
25    #[builder(default = "ClientConfig::default()")]
26    pub client: ClientConfig,
27    /// Configuration center configuration
28    #[serde(default)]
29    #[builder(setter(strip_option), default)]
30    pub config: Option<ConfigConfig>,
31    /// Registry center configuration
32    #[serde(default)]
33    #[builder(setter(strip_option), default)]
34    pub discovery: Option<DiscoveryConfig>,
35}
36
37impl Default for ConRegConfig {
38    fn default() -> Self {
39        ConRegConfig {
40            client: ClientConfig::default(),
41            service_id: utils::current_process_name(),
42            config: None,
43            discovery: None,
44        }
45    }
46}
47
48impl ConRegConfig {
49    fn default_service_id() -> String {
50        utils::current_process_name()
51    }
52}
53
54#[derive(Debug, Default, Deserialize, Clone)]
55#[serde(untagged)]
56pub enum ServerAddr {
57    Single(String),
58    Cluster(Vec<String>),
59    #[default]
60    Unset,
61}
62
63impl From<&str> for ServerAddr {
64    fn from(value: &str) -> Self {
65        ServerAddr::Single(value.to_string())
66    }
67}
68impl From<Vec<&str>> for ServerAddr {
69    fn from(value: Vec<&str>) -> Self {
70        ServerAddr::Cluster(value.into_iter().map(|s| s.to_string()).collect())
71    }
72}
73impl From<Vec<String>> for ServerAddr {
74    fn from(value: Vec<String>) -> Self {
75        ServerAddr::Cluster(value)
76    }
77}
78
79#[derive(Debug, Deserialize, Clone, Builder)]
80pub struct ClientConfig {
81    #[builder(setter(into), default = "ClientConfig::default_address()")]
82    pub address: String,
83    pub port: u16,
84}
85impl Default for ClientConfig {
86    fn default() -> Self {
87        ClientConfig {
88            address: ClientConfig::default_address(),
89            port: 8080,
90        }
91    }
92}
93
94impl ClientConfig {
95    pub fn gen_instance_id(&self) -> String {
96        let digest = md5::compute(format!("{}:{}", self.address, self.port));
97        format!("{:x}", digest)
98    }
99    pub fn default_address() -> String {
100        "127.0.0.1".to_string()
101    }
102}
103
104#[derive(Debug, Clone, Deserialize, Default, Builder)]
105#[serde(rename_all = "kebab-case")]
106pub struct ConfigConfig {
107    /// Configuration center address
108    #[builder(setter(into))]
109    pub server_addr: ServerAddr,
110    /// Namespace, default: public
111    #[serde(default = "ConfigConfig::default_namespace")]
112    #[builder(setter(into), default = "ConfigConfig::default_namespace()")]
113    pub namespace: String,
114    /// Configuration IDs, e.g.: `["application.yaml"]`
115    #[serde(default)]
116    pub config_ids: Vec<String>,
117    /// Namespace authentication token
118    #[builder(setter(into), default = "Default::default()")]
119    pub auth_token: Option<String>,
120}
121
122impl ConfigConfig {
123    /// Default namespace
124    fn default_namespace() -> String {
125        "public".to_string()
126    }
127}
128
129#[derive(Debug, Clone, Deserialize, Default, Builder)]
130#[serde(rename_all = "kebab-case")]
131pub struct DiscoveryConfig {
132    /// Configuration center address, e.g.: 127.0.0.1:8000
133    #[builder(setter(into))]
134    pub server_addr: ServerAddr,
135    /// Namespace, default: public
136    #[serde(default = "DiscoveryConfig::default_namespace")]
137    #[builder(setter(into), default = "DiscoveryConfig::default_namespace()")]
138    pub namespace: String,
139    /// Metadata
140    #[serde(default = "HashMap::default")]
141    #[builder(setter(into), default = "HashMap::default()")]
142    pub meta: HashMap<String, Value>,
143    /// Namespace authentication token
144    #[builder(setter(into), default = "Default::default()")]
145    pub auth_token: Option<String>,
146}
147
148impl DiscoveryConfig {
149    /// Default namespace
150    fn default_namespace() -> String {
151        "public".to_string()
152    }
153}