Skip to main content

systemprompt_models/config/
mod.rs

1//! Global runtime [`Config`] singleton and validation helpers.
2//!
3//! [`Config`] is the resolved, flat configuration installed once at
4//! startup into a process-wide `OnceLock` and read via [`Config::get`].
5//! Submodules cover environment classification, postgres-URL
6//! validation, rate-limit shapes, and verbosity levels.
7//! Accessors return [`crate::errors::ConfigError`] when not initialized.
8
9use std::path::PathBuf;
10use std::sync::OnceLock;
11use systemprompt_traits::ConfigProvider;
12
13use crate::auth::JwtAudience;
14use crate::profile::{ContentNegotiationConfig, SecurityHeadersConfig, TrustedIssuer};
15
16mod environment;
17mod paths;
18mod rate_limits;
19mod validation;
20mod verbosity;
21
22pub use environment::Environment;
23pub use paths::PathNotConfiguredError;
24pub use rate_limits::RateLimitConfig;
25pub use validation::validate_postgres_url;
26pub use verbosity::VerbosityLevel;
27
28static CONFIG: OnceLock<Config> = OnceLock::new();
29
30/// Default global cap on concurrent A2A SSE streams.
31pub const DEFAULT_MAX_CONCURRENT_STREAMS: usize = 256;
32
33/// Prefers the `HOSTNAME` environment variable (set by most container
34/// runtimes and shells); falls back to a generated short id when absent.
35#[must_use]
36pub fn default_instance_id() -> String {
37    std::env::var("HOSTNAME")
38        .ok()
39        .filter(|h| !h.trim().is_empty())
40        .unwrap_or_else(|| format!("instance-{}", uuid::Uuid::new_v4().simple()))
41}
42
43#[derive(Debug, Clone)]
44pub struct Config {
45    pub instance_id: String,
46    pub max_concurrent_streams: usize,
47    pub sitename: String,
48    pub database_type: String,
49    pub database_url: String,
50    pub database_write_url: Option<String>,
51    pub github_link: String,
52    pub github_token: Option<String>,
53    pub system_path: String,
54    pub services_path: String,
55    pub bin_path: String,
56    pub skills_path: String,
57    pub settings_path: String,
58    pub content_config_path: String,
59    pub geoip_database_path: Option<String>,
60    pub web_path: String,
61    pub web_config_path: String,
62    pub web_metadata_path: String,
63    pub host: String,
64    pub port: u16,
65    pub api_server_url: String,
66    pub api_internal_url: String,
67    pub api_external_url: String,
68    pub jwt_issuer: String,
69    pub jwt_access_token_expiration: i64,
70    pub jwt_refresh_token_expiration: i64,
71    pub jwt_audiences: Vec<JwtAudience>,
72    pub allowed_resource_audiences: Vec<String>,
73    pub trusted_issuers: Vec<TrustedIssuer>,
74    pub signing_key_path: PathBuf,
75    pub use_https: bool,
76    pub rate_limits: RateLimitConfig,
77    pub cors_allowed_origins: Vec<String>,
78    pub trusted_proxies: Vec<String>,
79    pub is_cloud: bool,
80    pub content_negotiation: ContentNegotiationConfig,
81    pub security_headers: SecurityHeadersConfig,
82    pub allow_registration: bool,
83    pub system_admin_username: String,
84}
85
86impl Config {
87    pub fn is_initialized() -> bool {
88        CONFIG.get().is_some()
89    }
90
91    pub fn get() -> Result<&'static Self, crate::errors::ConfigError> {
92        CONFIG
93            .get()
94            .ok_or(crate::errors::ConfigError::NotInitialized)
95    }
96
97    pub fn install(config: Self) -> Result<(), Box<Self>> {
98        CONFIG.set(config).map_err(Box::new)
99    }
100
101    pub fn logs_path(&self) -> String {
102        format!("{}/logs", self.system_path)
103    }
104}
105
106impl ConfigProvider for Config {
107    fn get(&self, key: &str) -> Option<String> {
108        match key {
109            "database_type" => Some(self.database_type.clone()),
110            "database_url" => Some(self.database_url.clone()),
111            "database_write_url" => self.database_write_url.clone(),
112            "host" => Some(self.host.clone()),
113            "port" => Some(self.port.to_string()),
114            "system_path" => Some(self.system_path.clone()),
115            "services_path" => Some(self.services_path.clone()),
116            "bin_path" => Some(self.bin_path.clone()),
117            "skills_path" => Some(self.skills_path.clone()),
118            "settings_path" => Some(self.settings_path.clone()),
119            "content_config_path" => Some(self.content_config_path.clone()),
120            "web_path" => Some(self.web_path.clone()),
121            "web_config_path" => Some(self.web_config_path.clone()),
122            "web_metadata_path" => Some(self.web_metadata_path.clone()),
123            "sitename" => Some(self.sitename.clone()),
124            "github_link" => Some(self.github_link.clone()),
125            "github_token" => self.github_token.clone(),
126            "api_server_url" => Some(self.api_server_url.clone()),
127            "api_external_url" => Some(self.api_external_url.clone()),
128            "jwt_issuer" => Some(self.jwt_issuer.clone()),
129            "is_cloud" => Some(self.is_cloud.to_string()),
130            "instance_id" => Some(self.instance_id.clone()),
131            "max_concurrent_streams" => Some(self.max_concurrent_streams.to_string()),
132            _ => None,
133        }
134    }
135
136    fn database_url(&self) -> &str {
137        &self.database_url
138    }
139
140    fn database_write_url(&self) -> Option<&str> {
141        self.database_write_url.as_deref()
142    }
143
144    fn system_path(&self) -> &str {
145        &self.system_path
146    }
147
148    fn api_port(&self) -> u16 {
149        self.port
150    }
151
152    fn as_any(&self) -> &dyn std::any::Any {
153        self
154    }
155}