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//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12use std::path::PathBuf;
13use std::sync::OnceLock;
14use systemprompt_traits::ConfigProvider;
15
16use crate::auth::JwtAudience;
17use crate::profile::{ContentNegotiationConfig, SecurityHeadersConfig, TrustedIssuer};
18
19mod environment;
20mod paths;
21mod rate_limits;
22mod validation;
23mod verbosity;
24
25pub use environment::Environment;
26pub use paths::PathNotConfiguredError;
27pub use rate_limits::RateLimitConfig;
28pub use validation::validate_postgres_url;
29pub use verbosity::VerbosityLevel;
30
31static CONFIG: OnceLock<Config> = OnceLock::new();
32
33pub const DEFAULT_MAX_CONCURRENT_STREAMS: usize = 256;
34
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 id_jag_ttl_secs: i64,
75    pub signing_key_path: PathBuf,
76    pub use_https: bool,
77    pub rate_limits: RateLimitConfig,
78    pub cors_allowed_origins: Vec<String>,
79    pub trusted_proxies: Vec<ipnet::IpNet>,
80    pub is_cloud: bool,
81    pub content_negotiation: ContentNegotiationConfig,
82    pub security_headers: SecurityHeadersConfig,
83    pub allow_registration: bool,
84    pub system_admin_username: String,
85    pub system_admin_email: Option<systemprompt_identifiers::Email>,
86}
87
88impl Config {
89    pub fn is_initialized() -> bool {
90        CONFIG.get().is_some()
91    }
92
93    pub fn get() -> Result<&'static Self, crate::errors::ConfigError> {
94        CONFIG
95            .get()
96            .ok_or(crate::errors::ConfigError::NotInitialized)
97    }
98
99    pub fn install(config: Self) -> Result<(), Box<Self>> {
100        CONFIG.set(config).map_err(Box::new)
101    }
102
103    pub fn logs_path(&self) -> String {
104        format!("{}/logs", self.system_path)
105    }
106}
107
108impl ConfigProvider for Config {
109    fn get(&self, key: &str) -> Option<String> {
110        match key {
111            "database_type" => Some(self.database_type.clone()),
112            "database_url" => Some(self.database_url.clone()),
113            "database_write_url" => self.database_write_url.clone(),
114            "host" => Some(self.host.clone()),
115            "port" => Some(self.port.to_string()),
116            "system_path" => Some(self.system_path.clone()),
117            "services_path" => Some(self.services_path.clone()),
118            "bin_path" => Some(self.bin_path.clone()),
119            "skills_path" => Some(self.skills_path.clone()),
120            "settings_path" => Some(self.settings_path.clone()),
121            "content_config_path" => Some(self.content_config_path.clone()),
122            "web_path" => Some(self.web_path.clone()),
123            "web_config_path" => Some(self.web_config_path.clone()),
124            "web_metadata_path" => Some(self.web_metadata_path.clone()),
125            "sitename" => Some(self.sitename.clone()),
126            "github_link" => Some(self.github_link.clone()),
127            "github_token" => self.github_token.clone(),
128            "api_server_url" => Some(self.api_server_url.clone()),
129            "api_external_url" => Some(self.api_external_url.clone()),
130            "jwt_issuer" => Some(self.jwt_issuer.clone()),
131            "is_cloud" => Some(self.is_cloud.to_string()),
132            "instance_id" => Some(self.instance_id.clone()),
133            "max_concurrent_streams" => Some(self.max_concurrent_streams.to_string()),
134            _ => None,
135        }
136    }
137
138    fn database_url(&self) -> &str {
139        &self.database_url
140    }
141
142    fn database_write_url(&self) -> Option<&str> {
143        self.database_write_url.as_deref()
144    }
145
146    fn system_path(&self) -> &str {
147        &self.system_path
148    }
149
150    fn api_port(&self) -> u16 {
151        self.port
152    }
153
154    fn as_any(&self) -> &dyn std::any::Any {
155        self
156    }
157}