Skip to main content

halephant_core/config/cluster/
mod.rs

1pub mod auth;
2pub mod pool;
3pub mod topology;
4
5use std::collections::HashMap;
6use std::time::Duration;
7
8use serde::Deserialize;
9
10use crate::errors;
11
12#[derive(Debug, Deserialize)]
13pub struct ClusterConfig {
14    pub nodes: Vec<String>,
15
16    #[serde(default = "default_admin_user")]
17    pub admin_user: String,
18
19    /// Database the admin connection selects in its startup message.
20    /// Defaults to `"postgres"`, which is cluster-global and carries
21    /// `pg_shadow` — enough for the default `auth.query`. Override
22    /// when `auth.query` references database-local objects such as a
23    /// custom role-mapping table that lives in a specific application
24    /// database.
25    #[serde(default = "default_admin_database")]
26    pub admin_database: String,
27
28    #[serde(default = "default_connect_timeout", with = "humantime_serde")]
29    pub connect_timeout: Duration,
30
31    #[serde(default)]
32    pub auth: auth::AuthConfig,
33
34    #[serde(default)]
35    pub topology: topology::TopologyConfig,
36
37    #[serde(default)]
38    pub pool: HashMap<String, pool::PoolConfig>,
39}
40
41impl ClusterConfig {
42    pub(crate) fn validate(&self, name: &str) -> Result<(), errors::ConfigError> {
43        if self.nodes.is_empty() {
44            return Err(errors::ConfigError::Validation(format!(
45                "cluster {name:?} has no nodes"
46            )));
47        }
48
49        for (db_name, pool) in &self.pool {
50            pool.validate(db_name)?;
51        }
52
53        Ok(())
54    }
55}
56
57// ---------------------------------------------------------------------------
58// Defaults
59// ---------------------------------------------------------------------------
60
61fn default_admin_user() -> String {
62    "halephant".into()
63}
64
65fn default_admin_database() -> String {
66    "postgres".into()
67}
68
69fn default_connect_timeout() -> Duration {
70    Duration::from_secs(5)
71}