Core/Config: Implement reading config overrides from subdirectory (#29068)

Co-authored-by: Shauren <shauren.trinity@gmail.com>
(cherry picked from commit 74a4dc46b4)
This commit is contained in:
Смердокрыл
2023-06-09 21:36:03 +03:00
committed by Shauren
parent 018e66bc91
commit 2fca554555
6 changed files with 101 additions and 28 deletions

View File

@@ -57,6 +57,9 @@ namespace fs = boost::filesystem;
#ifndef _TRINITY_REALM_CONFIG
# define _TRINITY_REALM_CONFIG "authserver.conf"
#endif
#ifndef _TRINITY_REALM_CONFIG_DIR
#define _TRINITY_REALM_CONFIG_DIR "authserver.conf.d"
#endif
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
#include "ServiceWin32.h"
@@ -79,7 +82,7 @@ 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)
{
@@ -91,18 +94,19 @@ int main(int argc, char** argv)
Trinity::Locale::Init();
auto configFile = fs::absolute(_TRINITY_REALM_CONFIG);
std::string configService;
auto vm = GetConsoleArguments(argc, argv, configFile, configService);
auto configDir = fs::absolute(_TRINITY_REALM_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;
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
if (configService.compare("install") == 0)
if (winServiceAction == "install")
return WinServiceInstall() == true ? 0 : 1;
else if (configService.compare("uninstall") == 0)
if (winServiceAction == "uninstall")
return WinServiceUninstall() == true ? 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>();
@@ -157,13 +175,16 @@ int main(int argc, char** argv)
if (!StartDB())
return 1;
std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
if (vm.count("update-databases-only"))
return 0;
sSecretMgr->Initialize();
// Load IP Location Database
sIPLocation->Load();
std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();
// Get the list of realms for the server
@@ -325,7 +346,7 @@ 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)
{
options_description all("Allowed options");
all.add_options()
@@ -333,16 +354,17 @@ variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, s
("version,v", "print version build info")
("config,c", value<fs::path>(&configFile)->default_value(fs::absolute(_TRINITY_REALM_CONFIG)),
"use <arg> as configuration file")
("config-dir,cd", value<fs::path>(&configDir)->default_value(fs::absolute(_TRINITY_REALM_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);
#else
(void)configService;
#endif
variables_map variablesMap;
try