Skip to main content

lrwf_core/
config.rs

1//! Built-in configuration support: appsettings.json + AppOptions pattern.
2//!
3//! The framework automatically loads `appsettings.json` (merged with
4//! `appsettings.Development.json` in dev mode) and binds it to the
5//! built-in `AppOptions` struct.  Users customize options via
6//! `HostBuilder::configure(|app| app.useOptions(|o| { ... }))`.
7
8use crate::mode::AppMode;
9use serde::Deserialize;
10use std::path::Path;
11
12// ---------------------------------------------------------------------------
13// IAppOptions trait (for user-defined option types)
14// ---------------------------------------------------------------------------
15
16/// Application options — binds to a section of appsettings.json.
17///
18/// Users define their own structs implementing this trait,
19/// then call `AppConfig::bind()` to bind values.
20pub trait IAppOptions: for<'de> Deserialize<'de> + Default + Send + Sync + 'static {}
21
22impl<T> IAppOptions for T where T: for<'de> Deserialize<'de> + Default + Send + Sync + 'static {}
23
24// ---------------------------------------------------------------------------
25// Built-in option types (matching standard appsettings.json layout)
26// ---------------------------------------------------------------------------
27
28/// Top-level application section.
29#[derive(Debug, Clone, Deserialize)]
30pub struct AppSection {
31    /// Application display name.
32    #[serde(default, rename = "Name")]
33    pub name: String,
34    /// Listen addresses as full URLs (e.g., "http://0.0.0.0:5000").
35    /// ASP.NET Core compatible. Default: ["http://0.0.0.0:5000"].
36    #[serde(default = "default_urls", rename = "Urls")]
37    pub urls: Vec<String>,
38    /// Maximum request body size in bytes. Default: 10 MB.
39    #[serde(default = "default_max_body_size", rename = "MaxBodySize")]
40    pub max_body_size: usize,
41}
42
43impl Default for AppSection {
44    fn default() -> Self {
45        Self {
46            name: String::new(),
47            urls: default_urls(),
48            max_body_size: default_max_body_size(),
49        }
50    }
51}
52
53fn default_urls() -> Vec<String> {
54    vec!["http://0.0.0.0:5000".to_string()]
55}
56
57fn default_max_body_size() -> usize {
58    10 * 1024 * 1024 // 10 MB
59}
60
61/// JWT authentication section.
62#[derive(Debug, Clone, Deserialize, Default)]
63pub struct JwtSection {
64    /// HMAC secret for signing/verifying JWT tokens.
65    #[serde(default, rename = "Secret")]
66    pub secret: String,
67}
68
69/// CORS (Cross-Origin Resource Sharing) section.
70#[derive(Debug, Clone, Deserialize)]
71pub struct CorsSection {
72    /// Allowed origins. Default: ["*"].
73    #[serde(default = "default_origins")]
74    pub origins: Vec<String>,
75    /// Allowed methods. Default: GET, POST, PUT, DELETE, PATCH, OPTIONS.
76    #[serde(default = "default_cors_methods")]
77    pub methods: Vec<String>,
78    /// Allowed headers. Default: Content-Type, Authorization.
79    #[serde(default = "default_cors_headers")]
80    pub headers: Vec<String>,
81    /// Allow credentials. Default: false.
82    #[serde(default)]
83    pub allow_credentials: bool,
84    /// Preflight cache max-age in seconds. Default: 86400.
85    #[serde(default = "default_max_age")]
86    pub max_age: u32,
87}
88
89impl Default for CorsSection {
90    fn default() -> Self {
91        Self {
92            origins: default_origins(),
93            methods: default_cors_methods(),
94            headers: default_cors_headers(),
95            allow_credentials: false,
96            max_age: default_max_age(),
97        }
98    }
99}
100
101fn default_origins() -> Vec<String> {
102    vec!["*".to_string()]
103}
104
105fn default_cors_methods() -> Vec<String> {
106    vec![
107        "GET".to_string(),
108        "POST".to_string(),
109        "PUT".to_string(),
110        "DELETE".to_string(),
111        "PATCH".to_string(),
112        "OPTIONS".to_string(),
113    ]
114}
115
116fn default_cors_headers() -> Vec<String> {
117    vec!["Content-Type".to_string(), "Authorization".to_string()]
118}
119
120fn default_max_age() -> u32 {
121    86400
122}
123
124/// TLS (Transport Layer Security) section.
125///
126/// TLS is activated automatically when the `App.Urls` array
127/// contains one or more `https://` entries. The certificate
128/// and key paths are read from this section.
129#[derive(Debug, Clone, Deserialize, Default)]
130pub struct TlsSection {
131    /// Path to TLS certificate PEM file.
132    #[serde(default, rename = "CertPath")]
133    pub cert_path: String,
134    /// Path to TLS private key PEM file.
135    #[serde(default, rename = "KeyPath")]
136    pub key_path: String,
137}
138
139/// Standard application options loaded from appsettings.json.
140///
141/// Bound automatically by the framework.  Access via `host.options()`
142/// or customize via `app.useOptions(|o| { ... })`.
143#[derive(Debug, Clone, Deserialize, Default)]
144pub struct AppOptions {
145    /// Application settings.
146    #[serde(default, rename = "App")]
147    pub app: AppSection,
148    /// JWT authentication settings.
149    #[serde(default, rename = "Jwt")]
150    pub jwt: JwtSection,
151    /// CORS settings.
152    #[serde(default, rename = "Cors")]
153    pub cors: CorsSection,
154    /// TLS settings.
155    #[serde(default, rename = "Tls")]
156    pub tls: TlsSection,
157}
158
159// ---------------------------------------------------------------------------
160// Config loading helpers
161// ---------------------------------------------------------------------------
162
163/// Load the merged appsettings JSON (base + Development overlay + env overrides).
164///
165/// Environment variables prefixed with `APP__` override the corresponding JSON values.
166/// For example, `APP__App__Address=0.0.0.0:8080` overrides `{"App": {"Address": "..."}}`.
167pub fn load_appsettings(mode: AppMode) -> Option<serde_json::Value> {
168    let mut base = read_json_file("appsettings.json")?;
169
170    if mode == AppMode::Development {
171        if let Some(dev) = read_json_file("appsettings.Development.json") {
172            merge_json(&mut base, dev);
173        }
174    }
175
176    // Apply environment variable overrides (APP__Section__Key pattern)
177    apply_env_overrides(&mut base);
178
179    Some(base)
180}
181
182/// Apply environment variable overrides following the `APP__Section__Key` pattern.
183fn apply_env_overrides(config: &mut serde_json::Value) {
184    for (key, value) in std::env::vars() {
185        if let Some(path) = key.strip_prefix("APP__") {
186            // Split by double underscore to get path segments
187            let segments: Vec<&str> = path.split("__").collect();
188            if segments.is_empty() {
189                continue;
190            }
191            set_json_value(config, &segments, &value);
192        }
193    }
194}
195
196/// Set a value in a JSON object following the given path segments.
197fn set_json_value(obj: &mut serde_json::Value, segments: &[&str], value: &str) {
198    if segments.is_empty() {
199        return;
200    }
201
202    let key = segments[0];
203
204    if let serde_json::Value::Object(map) = obj {
205        if segments.len() == 1 {
206            // Leaf: set the value, attempting to parse as JSON first
207            let parsed =
208                serde_json::from_str(value).unwrap_or(serde_json::Value::String(value.to_string()));
209            map.insert(key.to_string(), parsed);
210        } else if let Some(child) = map.get_mut(key) {
211            // Recurse into child
212            set_json_value(child, &segments[1..], value);
213        } else {
214            // Create intermediate objects as needed
215            let mut child = serde_json::json!({});
216            set_json_value(&mut child, &segments[1..], value);
217            map.insert(key.to_string(), child);
218        }
219    }
220}
221
222/// Bind a section of the config JSON to a deserializable type.
223pub fn bind_config<T: for<'de> Deserialize<'de> + Default>(
224    config: &serde_json::Value,
225    section: &str,
226) -> T {
227    if section.is_empty() || section == "." {
228        serde_json::from_value(config.clone()).unwrap_or_default()
229    } else {
230        config
231            .get(section)
232            .map(|v| serde_json::from_value(v.clone()).unwrap_or_default())
233            .unwrap_or_default()
234    }
235}
236
237/// Bind the entire config JSON to a type (for root-level deserialization).
238pub fn bind_root<T: for<'de> Deserialize<'de> + Default>(config: &serde_json::Value) -> T {
239    serde_json::from_value(config.clone()).unwrap_or_default()
240}
241
242// ---------------------------------------------------------------------------
243// Internal helpers
244// ---------------------------------------------------------------------------
245
246fn read_json_file(path: impl AsRef<Path>) -> Option<serde_json::Value> {
247    let path = path.as_ref();
248
249    /// Try to read and parse a JSON file at the given path.
250    fn try_read(path: &Path) -> Option<serde_json::Value> {
251        let content = std::fs::read_to_string(path).ok()?;
252        serde_json::from_str(&content).ok()
253    }
254
255    // 1. Try as-is (relative to current working directory)
256    if let Some(value) = try_read(path) {
257        return Some(value);
258    }
259
260    // 2. Walk up from cwd; at each ancestor, check its immediate
261    //    subdirectories.  This handles cargo workspace layouts where
262    //    config files live in a member crate (e.g.
263    //    workspace_root/demo/appsettings.json) and `cargo run` is
264    //    invoked from the workspace root.
265    if let Ok(cwd) = std::env::current_dir() {
266        let mut dir = Some(cwd.as_path());
267        while let Some(d) = dir {
268            if let Ok(entries) = std::fs::read_dir(d) {
269                for entry in entries.flatten() {
270                    if entry.path().is_dir() {
271                        let candidate = entry.path().join(path);
272                        if let Some(value) = try_read(&candidate) {
273                            return Some(value);
274                        }
275                    }
276                }
277            }
278            dir = d.parent();
279        }
280    }
281
282    None
283}
284
285fn merge_json(base: &mut serde_json::Value, overlay: serde_json::Value) {
286    match (base, overlay) {
287        (serde_json::Value::Object(a), serde_json::Value::Object(b)) => {
288            for (k, v) in b {
289                merge_json(a.entry(k).or_insert(serde_json::Value::Null), v);
290            }
291        }
292        (a, b) => *a = b,
293    }
294}