aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorСмердокрыл <smerdokryl@gmail.com>2023-06-09 21:36:03 +0300
committerGitHub <noreply@github.com>2023-06-09 20:36:03 +0200
commit74a4dc46b49e1902f44a36b5b3155ad103d8f3da (patch)
tree762c7f23d65608a2a26266b2e5c7b7a403e8e486 /src
parentc484530b0c4347821a4d62515576d1c8532bf9bb (diff)
Core/Config: Implement reading config overrides from subdirectory (#29068)
Co-authored-by: Shauren <shauren.trinity@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/common/Configuration/Config.cpp30
-rw-r--r--src/common/Configuration/Config.h2
-rw-r--r--src/server/bnetserver/CMakeLists.txt5
-rw-r--r--src/server/bnetserver/Main.cpp38
-rw-r--r--src/server/worldserver/CMakeLists.txt5
-rw-r--r--src/server/worldserver/Main.cpp42
6 files changed, 96 insertions, 26 deletions
diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp
index 93f7d367730..8af11fbf066 100644
--- a/src/common/Configuration/Config.cpp
+++ b/src/common/Configuration/Config.cpp
@@ -18,6 +18,7 @@
#include "Config.h"
#include "Log.h"
#include "StringConvert.h"
+#include <boost/filesystem/operations.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <algorithm>
#include <cstdlib>
@@ -25,6 +26,7 @@
#include <mutex>
namespace bpt = boost::property_tree;
+namespace fs = boost::filesystem;
namespace
{
@@ -158,6 +160,8 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
if (!LoadFile(file, fullTree, error))
return false;
+ std::lock_guard<std::mutex> lock(_configLock);
+
for (bpt::ptree::value_type const& child : fullTree.begin()->second)
_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);
@@ -167,6 +171,32 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
return true;
}
+bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector<std::string>& loadedFiles, std::vector<std::string>& errors)
+{
+ fs::path dirPath = dir;
+ if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
+ return true;
+
+ for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath))
+ {
+ if (!fs::is_regular_file(f))
+ continue;
+
+ fs::path configFile = fs::absolute(f);
+ if (configFile.extension() != ".conf")
+ continue;
+
+ std::string fileName = configFile.generic_string();
+ std::string error;
+ if (LoadAdditionalFile(fileName, keepOnReload, error))
+ loadedFiles.push_back(std::move(fileName));
+ else
+ errors.push_back(std::move(error));
+ }
+
+ return errors.empty();
+}
+
std::vector<std::string> ConfigMgr::OverrideWithEnvVariablesIfAny()
{
std::lock_guard<std::mutex> lock(_configLock);
diff --git a/src/common/Configuration/Config.h b/src/common/Configuration/Config.h
index 3a67563c1db..edf62eade83 100644
--- a/src/common/Configuration/Config.h
+++ b/src/common/Configuration/Config.h
@@ -20,6 +20,7 @@
#include "Define.h"
#include <string>
+#include <string_view>
#include <vector>
class TC_COMMON_API ConfigMgr
@@ -33,6 +34,7 @@ public:
/// Method used only for loading main configuration files (bnetserver.conf and worldserver.conf)
bool LoadInitial(std::string file, std::vector<std::string> args, std::string& error);
bool LoadAdditionalFile(std::string file, bool keepOnReload, std::string& error);
+ bool LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector<std::string>& loadedFiles, std::vector<std::string>& errors);
/// Overrides configuration with environment variables and returns overridden keys
std::vector<std::string> OverrideWithEnvVariablesIfAny();
diff --git a/src/server/bnetserver/CMakeLists.txt b/src/server/bnetserver/CMakeLists.txt
index c9b03c3247e..929fe52d3d3 100644
--- a/src/server/bnetserver/CMakeLists.txt
+++ b/src/server/bnetserver/CMakeLists.txt
@@ -35,8 +35,9 @@ add_executable(bnetserver
)
if (NOT WIN32)
- set_target_properties(bnetserver PROPERTIES
- COMPILE_DEFINITIONS _TRINITY_BNET_CONFIG="${CONF_DIR}/bnetserver.conf"
+ target_compile_definitions(bnetserver PRIVATE
+ _TRINITY_BNET_CONFIG="${CONF_DIR}/bnetserver.conf"
+ _TRINITY_BNET_CONFIG_DIR="${CONF_DIR}/bnetserver.conf.d"
)
endif()
diff --git a/src/server/bnetserver/Main.cpp b/src/server/bnetserver/Main.cpp
index 00fd64b3dd8..591f68e5343 100644
--- a/src/server/bnetserver/Main.cpp
+++ b/src/server/bnetserver/Main.cpp
@@ -58,6 +58,9 @@ namespace fs = boost::filesystem;
#ifndef _TRINITY_BNET_CONFIG
# define _TRINITY_BNET_CONFIG "bnetserver.conf"
#endif
+#ifndef _TRINITY_BNET_CONFIG_DIR
+ #define _TRINITY_BNET_CONFIG_DIR "bnetserver.conf.d"
+#endif
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
#include "ServiceWin32.h"
@@ -80,15 +83,16 @@ void StopDB();
void SignalHandler(std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error, int signalNumber);
void KeepDatabaseAliveHandler(std::weak_ptr<Trinity::Asio::DeadlineTimer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error);
void BanExpiryHandler(std::weak_ptr<Trinity::Asio::DeadlineTimer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error);
-variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService);
+variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, std::string& winServiceAction);
int main(int argc, char** argv)
{
signal(SIGABRT, &Trinity::AbortHandler);
auto configFile = fs::absolute(_TRINITY_BNET_CONFIG);
- std::string configService;
- auto vm = GetConsoleArguments(argc, argv, configFile, configService);
+ auto configDir = fs::absolute(_TRINITY_BNET_CONFIG_DIR);
+ std::string winServiceAction;
+ auto vm = GetConsoleArguments(argc, argv, configFile, configDir, winServiceAction);
// exit if help or version is enabled
if (vm.count("help") || vm.count("version"))
return 0;
@@ -98,11 +102,11 @@ int main(int argc, char** argv)
std::shared_ptr<void> protobufHandle(nullptr, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
- if (configService.compare("install") == 0)
+ if (winServiceAction == "install")
return WinServiceInstall() ? 0 : 1;
- else if (configService.compare("uninstall") == 0)
+ if (winServiceAction == "uninstall")
return WinServiceUninstall() ? 0 : 1;
- else if (configService.compare("run") == 0)
+ if (winServiceAction == "run")
return WinServiceRun() ? 0 : 1;
#endif
@@ -115,6 +119,20 @@ int main(int argc, char** argv)
return 1;
}
+ std::vector<std::string> loadedConfigFiles;
+ std::vector<std::string> configDirErrors;
+ bool additionalConfigFileLoadSuccess = sConfigMgr->LoadAdditionalDir(configDir.generic_string(), true, loadedConfigFiles, configDirErrors);
+ for (std::string const& loadedConfigFile : loadedConfigFiles)
+ printf("Loaded additional config file %s\n", loadedConfigFile.c_str());
+
+ if (!additionalConfigFileLoadSuccess)
+ {
+ for (std::string const& configDirError : configDirErrors)
+ printf("Error in additional config files: %s\n", configDirError.c_str());
+
+ return 1;
+ }
+
std::vector<std::string> overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny();
sLog->RegisterAppender<AppenderDB>();
@@ -337,22 +355,22 @@ void ServiceStatusWatcher(std::weak_ptr<Trinity::Asio::DeadlineTimer> serviceSta
}
#endif
-variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService)
+variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
{
- (void)configService;
-
options_description all("Allowed options");
all.add_options()
("help,h", "print usage message")
("version,v", "print version build info")
("config,c", value<fs::path>(&configFile)->default_value(fs::absolute(_TRINITY_BNET_CONFIG)),
"use <arg> as configuration file")
+ ("config-dir,cd", value<fs::path>(&configDir)->default_value(fs::absolute(_TRINITY_BNET_CONFIG_DIR)),
+ "use <arg> as directory with additional config files")
("update-databases-only,u", "updates databases only")
;
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
options_description win("Windows platform specific options");
win.add_options()
- ("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
+ ("service,s", value<std::string>(&winServiceAction)->default_value(""), "Windows service options: [install | uninstall]")
;
all.add(win);
diff --git a/src/server/worldserver/CMakeLists.txt b/src/server/worldserver/CMakeLists.txt
index 5f091d7ba0f..da3bd43c2d5 100644
--- a/src/server/worldserver/CMakeLists.txt
+++ b/src/server/worldserver/CMakeLists.txt
@@ -33,8 +33,9 @@ add_executable(worldserver
)
if(NOT WIN32)
- set_target_properties(worldserver PROPERTIES
- COMPILE_DEFINITIONS _TRINITY_CORE_CONFIG="${CONF_DIR}/worldserver.conf"
+ target_compile_definitions(worldserver PRIVATE
+ _TRINITY_CORE_CONFIG="${CONF_DIR}/worldserver.conf"
+ _TRINITY_CORE_CONFIG_DIR="${CONF_DIR}/worldserver.conf.d"
)
endif()
diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp
index cad929fa67f..d56fa0156da 100644
--- a/src/server/worldserver/Main.cpp
+++ b/src/server/worldserver/Main.cpp
@@ -73,6 +73,10 @@ namespace fs = boost::filesystem;
#define _TRINITY_CORE_CONFIG "worldserver.conf"
#endif
+#ifndef _TRINITY_CORE_CONFIG_DIR
+ #define _TRINITY_CORE_CONFIG_DIR "worldserver.conf.d"
+#endif
+
#ifdef _WIN32
#include "ServiceWin32.h"
char serviceName[] = "worldserver";
@@ -119,7 +123,7 @@ void WorldUpdateLoop();
void ClearOnlineAccounts();
void ShutdownCLIThread(std::thread* cliThread);
bool LoadRealmInfo();
-variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& cfg_service);
+variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, std::string& winServiceAction);
/// Launch the Trinity server
extern int main(int argc, char** argv)
@@ -127,9 +131,10 @@ extern int main(int argc, char** argv)
signal(SIGABRT, &Trinity::AbortHandler);
auto configFile = fs::absolute(_TRINITY_CORE_CONFIG);
- std::string configService;
+ auto configDir = fs::absolute(_TRINITY_CORE_CONFIG_DIR);
+ std::string winServiceAction;
- auto vm = GetConsoleArguments(argc, argv, configFile, configService);
+ auto vm = GetConsoleArguments(argc, argv, configFile, configDir, winServiceAction);
// exit if help or version is enabled
if (vm.count("help") || vm.count("version"))
return 0;
@@ -139,12 +144,12 @@ extern int main(int argc, char** argv)
std::shared_ptr<void> protobufHandle(nullptr, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
#ifdef _WIN32
- if (configService.compare("install") == 0)
+ if (winServiceAction == "install")
return WinServiceInstall() ? 0 : 1;
- else if (configService.compare("uninstall") == 0)
+ if (winServiceAction == "uninstall")
return WinServiceUninstall() ? 0 : 1;
- else if (configService.compare("run") == 0)
- return WinServiceRun() ? 0 : 0;
+ if (winServiceAction == "run")
+ return WinServiceRun() ? 0 : 1;
Optional<UINT> newTimerResolution;
boost::system::error_code dllError;
@@ -194,6 +199,20 @@ extern int main(int argc, char** argv)
return 1;
}
+ std::vector<std::string> loadedConfigFiles;
+ std::vector<std::string> configDirErrors;
+ bool additionalConfigFileLoadSuccess = sConfigMgr->LoadAdditionalDir(configDir.generic_string(), true, loadedConfigFiles, configDirErrors);
+ for (std::string const& loadedConfigFile : loadedConfigFiles)
+ printf("Loaded additional config file %s\n", loadedConfigFile.c_str());
+
+ if (!additionalConfigFileLoadSuccess)
+ {
+ for (std::string const& configDirError : configDirErrors)
+ printf("Error in additional config files: %s\n", configDirError.c_str());
+
+ return 1;
+ }
+
std::vector<std::string> overriddenKeys = sConfigMgr->OverrideWithEnvVariablesIfAny();
std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();
@@ -677,23 +696,22 @@ void ClearOnlineAccounts()
/// @}
-variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService)
+variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
{
- // Silences warning about configService not be used if the OS is not Windows
- (void)configService;
-
options_description all("Allowed options");
all.add_options()
("help,h", "print usage message")
("version,v", "print version build info")
("config,c", value<fs::path>(&configFile)->default_value(fs::absolute(_TRINITY_CORE_CONFIG)),
"use <arg> as configuration file")
+ ("config-dir,cd", value<fs::path>(&configDir)->default_value(fs::absolute(_TRINITY_CORE_CONFIG_DIR)),
+ "use <arg> as directory with additional config files")
("update-databases-only,u", "updates databases only")
;
#ifdef _WIN32
options_description win("Windows platform specific options");
win.add_options()
- ("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
+ ("service,s", value<std::string>(&winServiceAction)->default_value(""), "Windows service options: [install | uninstall]")
;
all.add(win);