diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index 7a51fc178..742777b18 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -32,6 +32,7 @@ namespace std::vector _additonalFiles; std::vector _args; std::unordered_map _configOptions; + std::unordered_map _envVarCache; std::mutex _configLock; // Check system configs like *server.conf* @@ -120,7 +121,7 @@ namespace auto const& itr = fileConfigs.find(confOption); if (itr != fileConfigs.end()) { - PrintError(file, "> Config::LoadFile: Dublicate key name '{}' in config file '{}'", confOption, file); + PrintError(file, "> Config::LoadFile: Duplicate key name '{}' in config file '{}'", confOption, file); return true; } @@ -283,9 +284,14 @@ namespace return result; } + std::string GetEnvVarName(std::string const& configName) + { + return "AC_" + IniKeyToEnvVarKey(configName); + } + Optional EnvVarForIniKey(std::string const& key) { - std::string envKey = "AC_" + IniKeyToEnvVarKey(key); + std::string envKey = GetEnvVarName(key); char* val = std::getenv(envKey.c_str()); if (!val) return std::nullopt; @@ -330,6 +336,29 @@ bool ConfigMgr::Reload() return true; } +// Check the _envVarCache if the env var is there +// if not, check the env for the value +Optional GetEnvFromCache(std::string const& configName, std::string const& envVarName) +{ + auto foundInCache = _envVarCache.find(envVarName); + Optional foundInEnv; + // If it's not in the cache + if (foundInCache == _envVarCache.end()) + { + // Check the env itself + foundInEnv = EnvVarForIniKey(configName); + if (foundInEnv) + { + // If it's found in the env, put it in the cache + _envVarCache.emplace(envVarName, *foundInEnv); + } + // Return the result of checking env + return foundInEnv; + } + + return foundInCache->second; +} + std::vector ConfigMgr::OverrideWithEnvVariablesIfAny() { std::lock_guard lock(_configLock); @@ -357,29 +386,31 @@ template T ConfigMgr::GetValueDefault(std::string const& name, T const& def, bool showLogs /*= true*/) const { std::string strValue; + auto const& itr = _configOptions.find(name); - if (itr == _configOptions.end()) + bool notFound = itr == _configOptions.end(); + auto envVarName = GetEnvVarName(name); + Optional envVar = GetEnvFromCache(name, envVarName); + if (envVar) { - Optional envVar = EnvVarForIniKey(name); - if (!envVar) + // If showLogs and this key/value pair wasn't found in the currently saved config + if (showLogs && (notFound || itr->second != envVar->c_str())) { - if (showLogs) - { - LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file.", - name, _filename, name, Acore::ToString(def)); - } - - return def; - } - - if (showLogs) - { - LOG_WARN("server.loading", "Missing property {} in config file {}, recovered with environment '{}' value.", - name, _filename, envVar->c_str()); + LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName ); + AddKey(name, envVar->c_str(), "ENVIRONMENT", false, false); } strValue = *envVar; } + else if (notFound) + { + if (showLogs) + { + LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.", + name, _filename, name, Acore::ToString(def), envVarName); + } + return def; + } else { strValue = itr->second; @@ -404,24 +435,26 @@ template<> std::string ConfigMgr::GetValueDefault(std::string const& name, std::string const& def, bool showLogs /*= true*/) const { auto const& itr = _configOptions.find(name); - if (itr == _configOptions.end()) + bool notFound = itr == _configOptions.end(); + auto envVarName = GetEnvVarName(name); + Optional envVar = GetEnvFromCache(name, envVarName); + if (envVar) { - Optional envVar = EnvVarForIniKey(name); - if (envVar) + // If showLogs and this key/value pair wasn't found in the currently saved config + if (showLogs && (notFound || itr->second != envVar->c_str())) { - if (showLogs) - { - LOG_WARN("server.loading", "Missing property {} in config file {}, recovered with environment '{}' value.", - name, _filename, envVar->c_str()); - } - - return *envVar; + LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName); + AddKey(name, *envVar, "ENVIRONMENT", false, false); } + return *envVar; + } + else if (notFound) + { if (showLogs) { - LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file.", - name, _filename, name, def); + LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.", + name, _filename, name, def, envVarName); } return def; diff --git a/src/server/apps/authserver/Main.cpp b/src/server/apps/authserver/Main.cpp index bc40f2f10..4c110db7a 100644 --- a/src/server/apps/authserver/Main.cpp +++ b/src/server/apps/authserver/Main.cpp @@ -85,8 +85,6 @@ int main(int argc, char** argv) if (!sConfigMgr->LoadAppConfigs()) return 1; - std::vector overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny(); - // Init logging sLog->RegisterAppender(); sLog->Initialize(nullptr); @@ -103,9 +101,6 @@ int main(int argc, char** argv) LOG_INFO("server.authserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100); }); - for (std::string const& key : overriddenKeys) - LOG_INFO("server.authserver", "Configuration field {} was overridden with environment variable.", key); - OpenSSLCrypto::threadsSetup(); std::shared_ptr opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); }); diff --git a/src/server/apps/worldserver/Main.cpp b/src/server/apps/worldserver/Main.cpp index fa91207a7..b2087448a 100644 --- a/src/server/apps/worldserver/Main.cpp +++ b/src/server/apps/worldserver/Main.cpp @@ -184,8 +184,6 @@ int main(int argc, char** argv) if (!sConfigMgr->LoadAppConfigs()) return 1; - std::vector overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny(); - std::shared_ptr ioContext = std::make_shared(); // Init all logs @@ -205,9 +203,6 @@ int main(int argc, char** argv) LOG_INFO("server.worldserver", "> Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100); }); - for (std::string const& key : overriddenKeys) - LOG_INFO("server.worldserver", "Configuration field {} was overridden with environment variable.", key); - OpenSSLCrypto::threadsSetup(); std::shared_ptr opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); });