Skip to main content

headless_lms_server/config/
program_config.rs

1use anyhow::Context;
2use headless_lms_base::config::bool_env_false_by_default;
3use std::env;
4
5pub struct ProgramConfig;
6
7impl ProgramConfig {
8    /// Reads DATABASE_URL with the historical development fallback.
9    pub fn database_url_with_default() -> String {
10        env::var("DATABASE_URL")
11            .unwrap_or_else(|_| "postgres://localhost/headless_lms_dev".to_string())
12    }
13
14    /// Reads a required environment variable by name.
15    pub fn required(key: &str) -> anyhow::Result<String> {
16        env::var(key).with_context(|| format!("{key} must be defined"))
17    }
18
19    /// Reads an optional environment variable by name.
20    pub fn optional(key: &str) -> Option<String> {
21        env::var(key).ok()
22    }
23
24    /// Reads a boolean env var where missing values default to false.
25    pub fn bool_flag(key: &str) -> bool {
26        bool_env_false_by_default(key)
27    }
28}