Own ConfigManager in Core; resolve config via DI instead of the singleton
Core holds a ConfigManager member, keeps it synced (updateConfig(config_)), and provides it as a service. ConfigManager.cpp drops the instance() definition. MusicManager gets the manager via setConfigManager() and reads sections through it (former ECHOION_BOT/YOUTUBE/NETWORK sites); RpcInfoCommand resolves it from the registry via ctx.service<Api::ConfigManager>(). Build green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
f7553d10d3
commit
0e08ccaefa
6 files changed
+29
-14
No files matched your search
@@ -5,6 +5,7 @@
|
|||||||
#include "MusicManager.hpp"
|
#include "MusicManager.hpp"
|
||||||
#include "NetworkClient.hpp"
|
#include "NetworkClient.hpp"
|
||||||
#include "RpcServer.hpp"
|
#include "RpcServer.hpp"
|
||||||
|
#include <ConfigManager.hpp>
|
||||||
#include <ConfigTypes.hpp>
|
#include <ConfigTypes.hpp>
|
||||||
#include <CoreInterface.hpp>
|
#include <CoreInterface.hpp>
|
||||||
#include <Void/Async/EventBus.hpp>
|
#include <Void/Async/EventBus.hpp>
|
||||||
@@ -192,6 +193,10 @@ namespace Echoion::Core
|
|||||||
// DB persistence. Owned by Core, provided as a service.
|
// DB persistence. Owned by Core, provided as a service.
|
||||||
Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async};
|
Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async};
|
||||||
|
|
||||||
|
// Config holder kept in sync with config_ and provided as a service
|
||||||
|
// (replaces the former ConfigManager singleton).
|
||||||
|
Api::ConfigManager configManager_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Access the service registry (composition root).
|
// Access the service registry (composition root).
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "RpcTypes.hpp"
|
#include "RpcTypes.hpp"
|
||||||
#include "SubscriptionManager.hpp"
|
#include "SubscriptionManager.hpp"
|
||||||
#include "YouTubeClient.hpp"
|
#include "YouTubeClient.hpp"
|
||||||
|
#include <ConfigManager.hpp>
|
||||||
#include <ConfigTypes.hpp>
|
#include <ConfigTypes.hpp>
|
||||||
#include <TrackTypes.hpp>
|
#include <TrackTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
@@ -41,6 +42,13 @@ namespace Echoion::Core
|
|||||||
// before playback so emitted events reach subscribers.
|
// before playback so emitted events reach subscribers.
|
||||||
void setEventBus(Void::EventBus::EventBus* bus) { eventBus_ = bus; }
|
void setEventBus(Void::EventBus::EventBus* bus) { eventBus_ = bus; }
|
||||||
|
|
||||||
|
// Inject the config holder (owned by Core). Must be set before
|
||||||
|
// initialize() so config sections resolve.
|
||||||
|
void setConfigManager(Api::ConfigManager* cfg)
|
||||||
|
{
|
||||||
|
configManager_ = cfg;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] Api::Result<std::string>
|
[[nodiscard]] Api::Result<std::string>
|
||||||
play(const std::string& query, uint64_t clientId);
|
play(const std::string& query, uint64_t clientId);
|
||||||
[[nodiscard]] Api::Result<void> pause();
|
[[nodiscard]] Api::Result<void> pause();
|
||||||
@@ -222,6 +230,8 @@ namespace Echoion::Core
|
|||||||
std::shared_ptr<NetworkClient> networkClient_;
|
std::shared_ptr<NetworkClient> networkClient_;
|
||||||
// Non-owning application event bus (provided by Core).
|
// Non-owning application event bus (provided by Core).
|
||||||
Void::EventBus::EventBus* eventBus_ = nullptr;
|
Void::EventBus::EventBus* eventBus_ = nullptr;
|
||||||
|
// Non-owning config holder (provided by Core).
|
||||||
|
Api::ConfigManager* configManager_ = nullptr;
|
||||||
std::unique_ptr<YouTubeClient> youTubeClient_;
|
std::unique_ptr<YouTubeClient> youTubeClient_;
|
||||||
std::unique_ptr<AudioDecoder> audioDecoder_;
|
std::unique_ptr<AudioDecoder> audioDecoder_;
|
||||||
std::unique_ptr<SubscriptionManager> subscriptionManager_;
|
std::unique_ptr<SubscriptionManager> subscriptionManager_;
|
||||||
|
|||||||
@@ -6,12 +6,6 @@
|
|||||||
namespace Echoion::Api
|
namespace Echoion::Api
|
||||||
{
|
{
|
||||||
|
|
||||||
ConfigManager& ConfigManager::instance()
|
|
||||||
{
|
|
||||||
static ConfigManager instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConfigManager::initialize(const std::filesystem::path& configPath)
|
bool ConfigManager::initialize(const std::filesystem::path& configPath)
|
||||||
{
|
{
|
||||||
std::unique_lock lock(mutex_);
|
std::unique_lock lock(mutex_);
|
||||||
|
|||||||
+3
-2
@@ -65,7 +65,7 @@ namespace Echoion::Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize global ConfigManager with loaded config
|
// Initialize global ConfigManager with loaded config
|
||||||
Api::ConfigManager::instance().updateConfig(config_, false);
|
configManager_.updateConfig(config_, false);
|
||||||
ECHOION_LOG_INFO("Global ConfigManager initialized");
|
ECHOION_LOG_INFO("Global ConfigManager initialized");
|
||||||
|
|
||||||
// Note: logLevel was removed from BotConfig, using Debug as default
|
// Note: logLevel was removed from BotConfig, using Debug as default
|
||||||
@@ -110,6 +110,7 @@ namespace Echoion::Core
|
|||||||
// Initialize music manager
|
// Initialize music manager
|
||||||
musicManager_ = std::make_shared<MusicManager>();
|
musicManager_ = std::make_shared<MusicManager>();
|
||||||
musicManager_->setEventBus(&eventBus_);
|
musicManager_->setEventBus(&eventBus_);
|
||||||
|
musicManager_->setConfigManager(&configManager_);
|
||||||
auto musicResult = musicManager_->initialize(networkClient_, database_);
|
auto musicResult = musicManager_->initialize(networkClient_, database_);
|
||||||
if (!musicResult) {
|
if (!musicResult) {
|
||||||
return std::unexpected(musicResult.error());
|
return std::unexpected(musicResult.error());
|
||||||
@@ -306,7 +307,7 @@ namespace Echoion::Core
|
|||||||
registry_.provide<Api::ILogger>(logger);
|
registry_.provide<Api::ILogger>(logger);
|
||||||
}
|
}
|
||||||
registry_.provide<Api::ConfigManager>(
|
registry_.provide<Api::ConfigManager>(
|
||||||
alias(&Api::ConfigManager::instance()));
|
alias(&configManager_));
|
||||||
registry_.provide<Void::EventBus::EventBus>(alias(&eventBus_));
|
registry_.provide<Void::EventBus::EventBus>(alias(&eventBus_));
|
||||||
if (networkClient_) {
|
if (networkClient_) {
|
||||||
registry_.provide<Api::INetworkClient>(networkClient_);
|
registry_.provide<Api::INetworkClient>(networkClient_);
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ namespace Echoion::Core
|
|||||||
database)
|
database)
|
||||||
{
|
{
|
||||||
// Get config from global ConfigManager
|
// Get config from global ConfigManager
|
||||||
const auto& botConfig = ECHOION_BOT;
|
const auto& botConfig = configManager_->getBot();
|
||||||
const auto& youtubeConfig = ECHOION_YOUTUBE;
|
const auto& youtubeConfig = configManager_->getYouTube();
|
||||||
const auto& networkConfig = ECHOION_NETWORK;
|
const auto& networkConfig = configManager_->getNetwork();
|
||||||
|
|
||||||
config_ = botConfig;
|
config_ = botConfig;
|
||||||
networkClient_ = std::move(networkClient);
|
networkClient_ = std::move(networkClient);
|
||||||
@@ -229,7 +229,7 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
// If direct open fails and we have a proxy, try downloading through
|
// If direct open fails and we have a proxy, try downloading through
|
||||||
// proxy
|
// proxy
|
||||||
const auto& networkConfig = ECHOION_NETWORK;
|
const auto& networkConfig = configManager_->getNetwork();
|
||||||
if (!openResult && networkConfig.proxy.has_value() &&
|
if (!openResult && networkConfig.proxy.has_value() &&
|
||||||
networkConfig.proxy->enabled()) {
|
networkConfig.proxy->enabled()) {
|
||||||
ECHOION_LOG_INFO(
|
ECHOION_LOG_INFO(
|
||||||
|
|||||||
@@ -74,8 +74,13 @@ namespace Echoion::Core
|
|||||||
|
|
||||||
void RpcInfoCommand::execute(const CommandContext& ctx)
|
void RpcInfoCommand::execute(const CommandContext& ctx)
|
||||||
{
|
{
|
||||||
// Get RPC configuration from global ConfigManager
|
// Resolve RPC configuration from the service registry.
|
||||||
const auto& networkConfig = ECHOION_NETWORK;
|
auto configManager = ctx.service<Api::ConfigManager>();
|
||||||
|
if (!configManager) {
|
||||||
|
ctx.reply("Configuration service unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto& networkConfig = configManager->getNetwork();
|
||||||
uint16_t rpcPort = networkConfig.rpcPort;
|
uint16_t rpcPort = networkConfig.rpcPort;
|
||||||
std::string rpcBindAddress = networkConfig.rpcBindAddress;
|
std::string rpcBindAddress = networkConfig.rpcBindAddress;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user