Make the service registry a real read path for commands

The DI registry was write-only — populated in Core::initialize() but never
resolved through. Thread it into command execution so commands resolve
shared services by interface instead of constructing throwaways:

- CommandContext gains a non-owning ServiceRegistry* plus an ergonomic
  service<I>() resolver.
- CommandManager carries the registry (Core::setServices(&registry_)) and
  stamps it onto every CommandContext it builds (async + sync paths).
- RpcInfoCommand's getPublicIpAddress() now reuses the shared, initialized
  INetworkClient from the registry, falling back to a throwaway client only
  when none is registered.

Core itself (the composition root) keeps direct member wiring and commands
keep explicit MusicManager injection — converting those to a service locator
would only hide dependencies; the registry earns its keep at the genuine
seam where a command needs a service it isn't handed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-22 22:25:15 +03:00
1 parent 6c7e6a170f
commit a9ab0d6b75
5 files changed
+46 -8

No files matched your search

+2
View File
@@ -224,6 +224,7 @@ namespace Echoion::Core
.args = std::move(argsStr),
.invokerName = task.invokerName,
.musicManager = task.musicManager,
.services = services_,
.replyCallback = std::move(task.replyFunc)};
command->execute(ctx);
@@ -366,6 +367,7 @@ namespace Echoion::Core
.args = std::move(argsStr),
.invokerName = invokerName,
.musicManager = musicManager,
.services = services_,
.replyCallback = std::move(replyFunc)};
try {
+4
View File
@@ -119,6 +119,10 @@ namespace Echoion::Core
commandManager_ = std::make_unique<CommandManager>();
commandManager_->setPrefix(config_.bot.commandPrefix);
commandManager_->setServerConfig(config_);
// Let commands resolve shared services (e.g. the network client) by
// interface from the registry composed below — the pointer is stable
// and entries are populated before any command dispatches.
commandManager_->setServices(&registry_);
// Set up permission check function
commandManager_->setPermissionCheckFunc(
+16 -8
View File
@@ -1,6 +1,7 @@
#include "echoion/core/NetworkClient.hpp"
#include "echoion/core/commands/RpcInfoCommand.hpp"
#include <ConfigManager.hpp>
#include <NetworkInterface.hpp>
#include <format>
#include <future>
#include <regex>
@@ -8,7 +9,7 @@
namespace Echoion::Core
{
std::string getPublicIpAddress()
std::string getPublicIpAddress(Api::INetworkClient* shared)
{
// Try to get public IP from external services
static std::string cachedIp;
@@ -27,11 +28,16 @@ namespace Echoion::Core
"https://api.ipify.org", "https://checkip.amazonaws.com",
"https://icanhazip.com", "https://ifconfig.me/ip"};
// Create a temporary network client for IP lookup
auto networkClient = std::make_shared<NetworkClient>();
auto initResult = networkClient->initialize();
if (!initResult) {
return cachedIp.empty() ? "127.0.0.1" : cachedIp;
// Prefer the shared, already-initialized network client resolved from
// the service registry; only spin up a throwaway one as a fallback.
std::shared_ptr<NetworkClient> owned;
Api::INetworkClient* networkClient = shared;
if (!networkClient) {
owned = std::make_shared<NetworkClient>();
if (!owned->initialize()) {
return cachedIp.empty() ? "127.0.0.1" : cachedIp;
}
networkClient = owned.get();
}
for (const auto& service : ipServices) {
@@ -73,8 +79,10 @@ namespace Echoion::Core
uint16_t rpcPort = networkConfig.rpcPort;
std::string rpcBindAddress = networkConfig.rpcBindAddress;
// Determine the public IP to show
std::string publicIp = getPublicIpAddress();
// Determine the public IP to show, reusing the shared network client
// from the registry when available.
auto sharedNet = ctx.service<Api::INetworkClient>();
std::string publicIp = getPublicIpAddress(sharedNet.get());
// If bind address is not localhost, use it
if (rpcBindAddress != "127.0.0.1" && rpcBindAddress != "localhost") {