| /** | |
| * @file config.cpp | |
| */ | |
| namespace app::core { | |
| std::expected<Config, std::string> Config::from_env() { | |
| Config cfg; | |
| if (const char* name = std::getenv("APP_NAME")) { | |
| cfg.app_name = name; | |
| } | |
| if (const char* url = std::getenv("DATABASE_URL")) { | |
| cfg.database_url = url; | |
| } | |
| if (const char* key = std::getenv("SECRET_KEY")) { | |
| cfg.secret_key = key; | |
| } | |
| if (const char* port_str = std::getenv("PORT")) { | |
| auto result = std::from_chars(port_str, port_str + std::strlen(port_str), cfg.port); | |
| if (result.ec != std::errc{}) { | |
| return std::unexpected<std::string>("Invalid PORT environment variable"); | |
| } | |
| } | |
| if (const char* debug = std::getenv("DEBUG")) { | |
| cfg.debug = std::string_view(debug) == "true" || std::string_view(debug) == "1"; | |
| } | |
| return cfg; | |
| } | |
| } // namespace app::core | |