Skip to main content

halephant_core/config/
mod.rs

1pub mod admin;
2pub mod cluster;
3pub mod logging;
4pub mod otel;
5pub mod reload;
6pub mod server;
7
8use std::collections::{HashMap, HashSet};
9use std::path::Path;
10
11use serde::Deserialize;
12
13use crate::errors;
14
15// ---------------------------------------------------------------------------
16// Top-level config
17// ---------------------------------------------------------------------------
18
19#[derive(Debug, Deserialize)]
20pub struct Config {
21    #[serde(default)]
22    pub server: server::ServerConfig,
23
24    #[serde(default)]
25    pub logging: logging::LoggingConfig,
26
27    #[serde(default)]
28    pub admin: admin::AdminConfig,
29
30    #[serde(default)]
31    pub otel: otel::OtelConfig,
32
33    #[serde(default)]
34    pub cluster: HashMap<String, cluster::ClusterConfig>,
35}
36
37impl Config {
38    /// Load and validate a config file.
39    pub fn load(path: &Path) -> Result<Self, errors::ConfigError> {
40        let content = std::fs::read_to_string(path)?;
41        Self::parse(&content)
42    }
43
44    /// Parse and validate a config from a TOML string.
45    pub fn parse(s: &str) -> Result<Self, errors::ConfigError> {
46        let config: Config = toml::from_str(s)?;
47        config.validate()?;
48        Ok(config)
49    }
50
51    /// Find the cluster name, cluster config, and pool config for a database.
52    pub fn find_pool(
53        &self,
54        database: &str,
55    ) -> Option<(&str, &cluster::ClusterConfig, &cluster::pool::PoolConfig)> {
56        for (name, cluster) in &self.cluster {
57            if let Some(pool) = cluster.pool.get(database) {
58                return Some((name, cluster, pool));
59            }
60        }
61        None
62    }
63
64    /// Check whether a user is allowed to connect to the given database.
65    pub fn is_user_allowed(&self, database: &str, user: &str) -> bool {
66        self.find_user(database, user).is_some()
67    }
68
69    /// Find the user config for a (database, user) pair.
70    pub fn find_user(
71        &self,
72        database: &str,
73        user: &str,
74    ) -> Option<&cluster::pool::user::UserConfig> {
75        let (_, _, pool) = self.find_pool(database)?;
76        pool.user.get(user)
77    }
78
79    fn validate(&self) -> Result<(), errors::ConfigError> {
80        self.server.validate()?;
81
82        // Database names must be unique across clusters — the PostgreSQL
83        // client protocol routes by database name alone.
84        let mut seen_databases = HashSet::new();
85        for cluster in self.cluster.values() {
86            for db_name in cluster.pool.keys() {
87                if !seen_databases.insert(db_name) {
88                    return Err(errors::ConfigError::Validation(format!(
89                        "database {db_name:?} appears in multiple clusters"
90                    )));
91                }
92            }
93        }
94
95        for (cluster_name, cluster) in &self.cluster {
96            cluster.validate(cluster_name)?;
97        }
98
99        Ok(())
100    }
101}