Migrate core infrastructure to VoidLibrary
This commit is contained in:
1 parent
53801f2b53
commit
2bdb63766d
65 files changed
+16109
-12011
No files matched your search
+424
-369
@@ -1,378 +1,433 @@
|
||||
#include "LoggerInterface.hpp"
|
||||
#include "Types.hpp"
|
||||
#include "echoion/core/ConfigLoader.hpp"
|
||||
#include "echoion/api/AudioTypes.hpp"
|
||||
#include <glaze/glaze.hpp>
|
||||
#include <fstream>
|
||||
#include <AudioTypes.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace Echoion::Core {
|
||||
// NOTE: The Echoion::Core::*Json mirror structs are plain reflectable
|
||||
// aggregates whose member names are exactly the desired JSON keys, so
|
||||
// Void::Json reflection (Json::to_json / Json::from_json) produces the same
|
||||
// document the former glz::meta specializations did — no annotations needed.
|
||||
|
||||
// JSON structures for configuration
|
||||
struct BotConfigJson {
|
||||
std::string botName = "Echoion";
|
||||
std::string commandPrefix = "!";
|
||||
float defaultVolume = 0.5f;
|
||||
bool autoReconnect = true;
|
||||
uint32_t reconnectDelay = 5000;
|
||||
uint32_t maxReconnectAttempts = 5;
|
||||
std::optional<std::string> proxyHost;
|
||||
std::optional<uint16_t> proxyPort;
|
||||
std::optional<std::string> proxyUsername;
|
||||
std::optional<std::string> proxyPassword;
|
||||
std::optional<std::string> proxyType;
|
||||
std::optional<std::string> youtubeApiKey;
|
||||
std::string preferredQuality = "High";
|
||||
bool allowPlaylistImports = true;
|
||||
uint32_t maxQueueSize = 100;
|
||||
uint32_t trackTimeout = 30000;
|
||||
std::optional<std::string> youtubeCookiesPath;
|
||||
std::optional<std::string> cookiesFromBrowser;
|
||||
uint32_t ytDlpMaxConcurrent = 4;
|
||||
bool ytDlpAsyncEnabled = true;
|
||||
bool enableUrlShortening = true;
|
||||
};
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
struct ServerConfigJson {
|
||||
std::string address;
|
||||
uint16_t port = 9987;
|
||||
std::string serverPassword = "";
|
||||
std::string nickname = "EchoionBot";
|
||||
std::optional<std::string> defaultChannelId;
|
||||
std::optional<std::string> channelPassword;
|
||||
std::vector<uint64_t> adminGroups;
|
||||
std::vector<uint64_t> djGroups;
|
||||
std::vector<uint64_t> bannedGroups;
|
||||
};
|
||||
Api::Result<Api::AppConfig>
|
||||
ConfigLoader::loadFromFile(const std::filesystem::path& path)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::NotFound,
|
||||
std::format("Configuration file not found: {}",
|
||||
path.string())));
|
||||
}
|
||||
|
||||
struct SecurityConfigJson {
|
||||
bool requireAuthForAdmin = true;
|
||||
uint32_t maxLoginAttempts = 5;
|
||||
uint32_t lockoutDuration = 300000;
|
||||
uint32_t sessionTimeout = 3600000;
|
||||
bool enableAuditLog = true;
|
||||
};
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::PermissionDenied,
|
||||
std::format("Failed to open configuration file: {}",
|
||||
path.string())));
|
||||
}
|
||||
|
||||
struct AppConfigJson {
|
||||
BotConfigJson bot;
|
||||
ServerConfigJson server;
|
||||
SecurityConfigJson security;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
|
||||
// Glaze metadata for JSON serialization
|
||||
template<> struct glz::meta<Echoion::Core::BotConfigJson> {
|
||||
using T = Echoion::Core::BotConfigJson;
|
||||
static constexpr auto value = object(
|
||||
"botName", &T::botName,
|
||||
"commandPrefix", &T::commandPrefix,
|
||||
"defaultVolume", &T::defaultVolume,
|
||||
"autoReconnect", &T::autoReconnect,
|
||||
"reconnectDelay", &T::reconnectDelay,
|
||||
"maxReconnectAttempts", &T::maxReconnectAttempts,
|
||||
"proxyHost", &T::proxyHost,
|
||||
"proxyPort", &T::proxyPort,
|
||||
"proxyUsername", &T::proxyUsername,
|
||||
"proxyPassword", &T::proxyPassword,
|
||||
"proxyType", &T::proxyType,
|
||||
"youtubeApiKey", &T::youtubeApiKey,
|
||||
"preferredQuality", &T::preferredQuality,
|
||||
"allowPlaylistImports", &T::allowPlaylistImports,
|
||||
"maxQueueSize", &T::maxQueueSize,
|
||||
"trackTimeout", &T::trackTimeout,
|
||||
"youtubeCookiesPath", &T::youtubeCookiesPath,
|
||||
"cookiesFromBrowser", &T::cookiesFromBrowser,
|
||||
"ytDlpMaxConcurrent", &T::ytDlpMaxConcurrent,
|
||||
"ytDlpAsyncEnabled", &T::ytDlpAsyncEnabled,
|
||||
"enableUrlShortening", &T::enableUrlShortening
|
||||
);
|
||||
};
|
||||
|
||||
template<> struct glz::meta<Echoion::Core::ServerConfigJson> {
|
||||
using T = Echoion::Core::ServerConfigJson;
|
||||
static constexpr auto value = object(
|
||||
"address", &T::address,
|
||||
"port", &T::port,
|
||||
"serverPassword", &T::serverPassword,
|
||||
"nickname", &T::nickname,
|
||||
"defaultChannelId", &T::defaultChannelId,
|
||||
"channelPassword", &T::channelPassword,
|
||||
"adminGroups", &T::adminGroups,
|
||||
"djGroups", &T::djGroups,
|
||||
"bannedGroups", &T::bannedGroups
|
||||
);
|
||||
};
|
||||
|
||||
template<> struct glz::meta<Echoion::Core::SecurityConfigJson> {
|
||||
using T = Echoion::Core::SecurityConfigJson;
|
||||
static constexpr auto value = object(
|
||||
"requireAuthForAdmin", &T::requireAuthForAdmin,
|
||||
"maxLoginAttempts", &T::maxLoginAttempts,
|
||||
"lockoutDuration", &T::lockoutDuration,
|
||||
"sessionTimeout", &T::sessionTimeout,
|
||||
"enableAuditLog", &T::enableAuditLog
|
||||
);
|
||||
};
|
||||
|
||||
template<> struct glz::meta<Echoion::Core::AppConfigJson> {
|
||||
using T = Echoion::Core::AppConfigJson;
|
||||
static constexpr auto value = object(
|
||||
"bot", &T::bot,
|
||||
"server", &T::server,
|
||||
"security", &T::security
|
||||
);
|
||||
};
|
||||
|
||||
namespace Echoion::Core {
|
||||
|
||||
Api::Result<Api::AppConfig> ConfigLoader::loadFromFile(const std::filesystem::path& path) {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::NotFound,
|
||||
std::format("Configuration file not found: {}", path.string())));
|
||||
}
|
||||
|
||||
std::ifstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::PermissionDenied,
|
||||
std::format("Failed to open configuration file: {}", path.string())));
|
||||
}
|
||||
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
AppConfigJson jsonConfig;
|
||||
auto ec = glz::read_json(jsonConfig, content);
|
||||
if (ec) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
std::format("Failed to parse configuration: {}", glz::format_error(ec, content))));
|
||||
}
|
||||
|
||||
// Convert to API config
|
||||
Api::AppConfig config;
|
||||
|
||||
// Bot config
|
||||
config.bot.botName = jsonConfig.bot.botName;
|
||||
config.bot.commandPrefix = jsonConfig.bot.commandPrefix;
|
||||
config.bot.defaultVolume = jsonConfig.bot.defaultVolume;
|
||||
config.bot.autoReconnect = jsonConfig.bot.autoReconnect;
|
||||
config.bot.reconnectDelay = std::chrono::milliseconds(jsonConfig.bot.reconnectDelay);
|
||||
config.bot.maxReconnectAttempts = jsonConfig.bot.maxReconnectAttempts;
|
||||
config.bot.preferredQuality = parseQuality(jsonConfig.bot.preferredQuality);
|
||||
config.bot.allowPlaylistImports = jsonConfig.bot.allowPlaylistImports;
|
||||
config.bot.maxQueueSize = jsonConfig.bot.maxQueueSize;
|
||||
config.bot.trackTimeout = std::chrono::milliseconds(jsonConfig.bot.trackTimeout);
|
||||
|
||||
// Proxy config
|
||||
if (jsonConfig.bot.proxyHost.has_value() && jsonConfig.bot.proxyPort.has_value()) {
|
||||
Api::ProxyConfig proxy;
|
||||
proxy.host = *jsonConfig.bot.proxyHost;
|
||||
proxy.port = *jsonConfig.bot.proxyPort;
|
||||
proxy.username = jsonConfig.bot.proxyUsername;
|
||||
proxy.password = jsonConfig.bot.proxyPassword;
|
||||
// Parse proxy type (default to HTTP)
|
||||
if (jsonConfig.bot.proxyType.has_value()) {
|
||||
const auto& typeStr = *jsonConfig.bot.proxyType;
|
||||
if (typeStr == "SOCKS5" || typeStr == "socks5") {
|
||||
proxy.type = Api::ProxyType::SOCKS5;
|
||||
} else {
|
||||
proxy.type = Api::ProxyType::HTTP;
|
||||
}
|
||||
} else {
|
||||
proxy.type = Api::ProxyType::HTTP;
|
||||
}
|
||||
config.bot.proxy = proxy;
|
||||
}
|
||||
|
||||
// YouTube API key
|
||||
if (jsonConfig.bot.youtubeApiKey.has_value()) {
|
||||
config.bot.youtubeApiKey = jsonConfig.bot.youtubeApiKey;
|
||||
}
|
||||
|
||||
// YouTube cookies
|
||||
if (jsonConfig.bot.youtubeCookiesPath.has_value()) {
|
||||
config.bot.youtubeCookiesPath = jsonConfig.bot.youtubeCookiesPath;
|
||||
}
|
||||
if (jsonConfig.bot.cookiesFromBrowser.has_value()) {
|
||||
config.bot.cookiesFromBrowser = jsonConfig.bot.cookiesFromBrowser;
|
||||
}
|
||||
|
||||
// yt-dlp settings
|
||||
config.bot.ytDlpMaxConcurrent = std::min(static_cast<size_t>(jsonConfig.bot.ytDlpMaxConcurrent), size_t(8));
|
||||
config.bot.ytDlpAsyncEnabled = jsonConfig.bot.ytDlpAsyncEnabled;
|
||||
|
||||
// URL shortening settings
|
||||
config.bot.enableUrlShortening = jsonConfig.bot.enableUrlShortening;
|
||||
|
||||
// Server config
|
||||
config.server.address = jsonConfig.server.address;
|
||||
config.server.port = jsonConfig.server.port;
|
||||
config.server.serverPassword = jsonConfig.server.serverPassword;
|
||||
config.server.nickname = jsonConfig.server.nickname;
|
||||
config.server.defaultChannelId = jsonConfig.server.defaultChannelId;
|
||||
config.server.channelPassword = jsonConfig.server.channelPassword;
|
||||
config.server.adminGroups = jsonConfig.server.adminGroups;
|
||||
config.server.djGroups = jsonConfig.server.djGroups;
|
||||
config.server.bannedGroups = jsonConfig.server.bannedGroups;
|
||||
|
||||
// Security config
|
||||
config.security.requireAuthForAdmin = jsonConfig.security.requireAuthForAdmin;
|
||||
config.security.maxLoginAttempts = jsonConfig.security.maxLoginAttempts;
|
||||
config.security.lockoutDuration = std::chrono::milliseconds(jsonConfig.security.lockoutDuration);
|
||||
config.security.sessionTimeout = std::chrono::milliseconds(jsonConfig.security.sessionTimeout);
|
||||
config.security.enableAuditLog = jsonConfig.security.enableAuditLog;
|
||||
|
||||
ECHOION_LOG_INFO(std::format("Configuration loaded from {}", path.string()));
|
||||
return config;
|
||||
}
|
||||
|
||||
Api::Result<void> ConfigLoader::saveToFile(const std::filesystem::path& path, const Api::AppConfig& config) {
|
||||
AppConfigJson jsonConfig;
|
||||
|
||||
// Bot config
|
||||
jsonConfig.bot.botName = config.bot.botName;
|
||||
jsonConfig.bot.commandPrefix = config.bot.commandPrefix;
|
||||
jsonConfig.bot.defaultVolume = config.bot.defaultVolume;
|
||||
jsonConfig.bot.autoReconnect = config.bot.autoReconnect;
|
||||
jsonConfig.bot.reconnectDelay = static_cast<uint32_t>(config.bot.reconnectDelay.count());
|
||||
jsonConfig.bot.maxReconnectAttempts = config.bot.maxReconnectAttempts;
|
||||
jsonConfig.bot.preferredQuality = qualityToString(config.bot.preferredQuality);
|
||||
jsonConfig.bot.allowPlaylistImports = config.bot.allowPlaylistImports;
|
||||
jsonConfig.bot.maxQueueSize = config.bot.maxQueueSize;
|
||||
jsonConfig.bot.trackTimeout = static_cast<uint32_t>(config.bot.trackTimeout.count());
|
||||
|
||||
// Proxy config
|
||||
if (config.bot.proxy.has_value()) {
|
||||
jsonConfig.bot.proxyHost = config.bot.proxy->host;
|
||||
jsonConfig.bot.proxyPort = config.bot.proxy->port;
|
||||
jsonConfig.bot.proxyUsername = config.bot.proxy->username;
|
||||
jsonConfig.bot.proxyPassword = config.bot.proxy->password;
|
||||
jsonConfig.bot.proxyType = config.bot.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5" : "HTTP";
|
||||
}
|
||||
|
||||
// YouTube API key
|
||||
if (config.bot.youtubeApiKey.has_value()) {
|
||||
jsonConfig.bot.youtubeApiKey = *config.bot.youtubeApiKey;
|
||||
}
|
||||
|
||||
// YouTube cookies
|
||||
if (config.bot.youtubeCookiesPath.has_value()) {
|
||||
jsonConfig.bot.youtubeCookiesPath = *config.bot.youtubeCookiesPath;
|
||||
}
|
||||
if (config.bot.cookiesFromBrowser.has_value()) {
|
||||
jsonConfig.bot.cookiesFromBrowser = *config.bot.cookiesFromBrowser;
|
||||
}
|
||||
|
||||
// yt-dlp settings
|
||||
jsonConfig.bot.ytDlpMaxConcurrent = static_cast<uint32_t>(config.bot.ytDlpMaxConcurrent);
|
||||
jsonConfig.bot.ytDlpAsyncEnabled = config.bot.ytDlpAsyncEnabled;
|
||||
|
||||
// URL shortening settings
|
||||
jsonConfig.bot.enableUrlShortening = config.bot.enableUrlShortening;
|
||||
|
||||
// Server config
|
||||
jsonConfig.server.address = config.server.address;
|
||||
jsonConfig.server.port = config.server.port;
|
||||
jsonConfig.server.serverPassword = config.server.serverPassword;
|
||||
jsonConfig.server.nickname = config.server.nickname;
|
||||
jsonConfig.server.defaultChannelId = config.server.defaultChannelId;
|
||||
jsonConfig.server.channelPassword = config.server.channelPassword;
|
||||
jsonConfig.server.adminGroups = config.server.adminGroups;
|
||||
jsonConfig.server.djGroups = config.server.djGroups;
|
||||
jsonConfig.server.bannedGroups = config.server.bannedGroups;
|
||||
|
||||
// Security config
|
||||
jsonConfig.security.requireAuthForAdmin = config.security.requireAuthForAdmin;
|
||||
jsonConfig.security.maxLoginAttempts = config.security.maxLoginAttempts;
|
||||
jsonConfig.security.lockoutDuration = static_cast<uint32_t>(config.security.lockoutDuration.count());
|
||||
jsonConfig.security.sessionTimeout = static_cast<uint32_t>(config.security.sessionTimeout.count());
|
||||
jsonConfig.security.enableAuditLog = config.security.enableAuditLog;
|
||||
|
||||
auto json = glz::write_json(jsonConfig);
|
||||
if (!json.has_value())
|
||||
{
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
std::format("Failed to serialize: {}", json.error().custom_error_message)));
|
||||
}
|
||||
std::ofstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::PermissionDenied,
|
||||
std::format("Failed to write configuration file: {}", path.string())));
|
||||
}
|
||||
|
||||
file << json.value();
|
||||
file.close();
|
||||
|
||||
ECHOION_LOG_INFO(std::format("Configuration saved to {}", path.string()));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::filesystem::path ConfigLoader::getDefaultConfigPath() {
|
||||
// Try to get config path from environment or use default
|
||||
const char* configHome = std::getenv("XDG_CONFIG_HOME");
|
||||
if (configHome) {
|
||||
return std::filesystem::path(configHome) / "echoion" / "config.json";
|
||||
}
|
||||
|
||||
const char* home = std::getenv("HOME");
|
||||
if (home) {
|
||||
return std::filesystem::path(home) / ".config" / "echoion" / "config.json";
|
||||
}
|
||||
|
||||
// Fallback to current directory
|
||||
return "echoion_config.json";
|
||||
}
|
||||
|
||||
Api::AppConfig ConfigLoader::createDefaultConfig() {
|
||||
Api::AppConfig config;
|
||||
|
||||
// Default bot config
|
||||
config.bot.botName = "Echoion";
|
||||
config.bot.commandPrefix = "!";
|
||||
config.bot.defaultVolume = 0.5f;
|
||||
config.bot.autoReconnect = true;
|
||||
config.bot.reconnectDelay = std::chrono::milliseconds(5000);
|
||||
config.bot.maxReconnectAttempts = 5;
|
||||
config.bot.preferredQuality = Api::AudioQuality::High;
|
||||
config.bot.allowPlaylistImports = true;
|
||||
config.bot.maxQueueSize = 100;
|
||||
config.bot.trackTimeout = std::chrono::milliseconds(30000);
|
||||
config.bot.ytDlpMaxConcurrent = 4;
|
||||
config.bot.ytDlpAsyncEnabled = true;
|
||||
config.bot.enableUrlShortening = true;
|
||||
|
||||
// Default server config
|
||||
config.server.address = "localhost";
|
||||
config.server.port = 9987;
|
||||
config.server.nickname = "EchoionBot";
|
||||
|
||||
// Default security config
|
||||
config.security.requireAuthForAdmin = true;
|
||||
config.security.maxLoginAttempts = 5;
|
||||
config.security.lockoutDuration = std::chrono::milliseconds(300000);
|
||||
config.security.sessionTimeout = std::chrono::milliseconds(3600000);
|
||||
config.security.enableAuditLog = true;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
Api::AudioQuality ConfigLoader::parseQuality(const std::string& str) {
|
||||
if (str == "Low") return Api::AudioQuality::Low;
|
||||
if (str == "Medium") return Api::AudioQuality::Medium;
|
||||
if (str == "High") return Api::AudioQuality::High;
|
||||
if (str == "VeryHigh") return Api::AudioQuality::VeryHigh;
|
||||
if (str == "Lossless") return Api::AudioQuality::Lossless;
|
||||
return Api::AudioQuality::High; // Default
|
||||
}
|
||||
|
||||
std::string ConfigLoader::qualityToString(Api::AudioQuality quality) {
|
||||
switch (quality) {
|
||||
case Api::AudioQuality::Low: return "Low";
|
||||
case Api::AudioQuality::Medium: return "Medium";
|
||||
case Api::AudioQuality::High: return "High";
|
||||
case Api::AudioQuality::VeryHigh: return "VeryHigh";
|
||||
case Api::AudioQuality::Lossless: return "Lossless";
|
||||
default: return "High";
|
||||
}
|
||||
}
|
||||
std::string content((std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>());
|
||||
file.close();
|
||||
|
||||
auto parsed = Void::Json::parse(content);
|
||||
if (!parsed) {
|
||||
return std::unexpected(Api::Error(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("Failed to parse configuration ({}:{}): {}",
|
||||
parsed.error().line, parsed.error().col,
|
||||
parsed.error().message)));
|
||||
}
|
||||
|
||||
// Tolerant deserialization: unknown JSON fields are ignored and missing
|
||||
// fields keep their default-initialized values (matches the former
|
||||
// glz::opts{error_on_unknown_keys=false, error_on_missing_keys=false}).
|
||||
auto jsonResult = Void::Json::from_json<AppConfigJson>(*parsed);
|
||||
if (!jsonResult) {
|
||||
return std::unexpected(Api::Error(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("Failed to parse configuration: field '{}': {}",
|
||||
jsonResult.error().field,
|
||||
jsonResult.error().message)));
|
||||
}
|
||||
AppConfigJson jsonConfig = std::move(*jsonResult);
|
||||
|
||||
// Convert to API config
|
||||
Api::AppConfig config;
|
||||
|
||||
// Bot config
|
||||
config.bot.botName = jsonConfig.bot.botName;
|
||||
config.bot.commandPrefix = jsonConfig.bot.commandPrefix;
|
||||
config.bot.defaultVolume = jsonConfig.bot.defaultVolume;
|
||||
config.bot.maxQueueSize = jsonConfig.bot.maxQueueSize;
|
||||
config.bot.trackTimeout =
|
||||
std::chrono::milliseconds(jsonConfig.bot.trackTimeout);
|
||||
config.bot.autoReconnect = jsonConfig.bot.autoReconnect;
|
||||
config.bot.reconnectDelay =
|
||||
std::chrono::milliseconds(jsonConfig.bot.reconnectDelay);
|
||||
config.bot.maxReconnectAttempts = jsonConfig.bot.maxReconnectAttempts;
|
||||
config.bot.preferredQuality =
|
||||
parseQuality(jsonConfig.bot.preferredQuality);
|
||||
config.bot.allowPlaylistImports = jsonConfig.bot.allowPlaylistImports;
|
||||
|
||||
// YouTube config
|
||||
if (jsonConfig.youtube.youtubeApiKey.has_value()) {
|
||||
config.youtube.youtubeApiKey = jsonConfig.youtube.youtubeApiKey;
|
||||
}
|
||||
if (jsonConfig.youtube.youtubeCookiesPath.has_value()) {
|
||||
config.youtube.youtubeCookiesPath =
|
||||
jsonConfig.youtube.youtubeCookiesPath;
|
||||
}
|
||||
if (jsonConfig.youtube.cookiesFromBrowser.has_value()) {
|
||||
config.youtube.cookiesFromBrowser =
|
||||
jsonConfig.youtube.cookiesFromBrowser;
|
||||
}
|
||||
config.youtube.ytDlpMaxConcurrent =
|
||||
std::min(static_cast<size_t>(jsonConfig.youtube.ytDlpMaxConcurrent),
|
||||
size_t(8));
|
||||
config.youtube.ytDlpAsyncEnabled = jsonConfig.youtube.ytDlpAsyncEnabled;
|
||||
config.youtube.enableUrlShortening =
|
||||
jsonConfig.youtube.enableUrlShortening;
|
||||
|
||||
// Network config
|
||||
if (jsonConfig.network.proxyHost.has_value() &&
|
||||
jsonConfig.network.proxyPort.has_value()) {
|
||||
Api::ProxyConfig proxy;
|
||||
proxy.host = *jsonConfig.network.proxyHost;
|
||||
proxy.port = *jsonConfig.network.proxyPort;
|
||||
proxy.username = jsonConfig.network.proxyUsername;
|
||||
proxy.password = jsonConfig.network.proxyPassword;
|
||||
const auto& typeStr = jsonConfig.network.proxyType;
|
||||
if (typeStr == "SOCKS5" || typeStr == "socks5") {
|
||||
proxy.type = Api::ProxyType::SOCKS5;
|
||||
} else {
|
||||
proxy.type = Api::ProxyType::HTTP;
|
||||
}
|
||||
config.network.proxy = proxy;
|
||||
}
|
||||
config.network.rpcEnabled = jsonConfig.network.rpcEnabled;
|
||||
config.network.rpcPort = jsonConfig.network.rpcPort;
|
||||
config.network.rpcBindAddress = jsonConfig.network.rpcBindAddress;
|
||||
|
||||
// TeamSpeak config
|
||||
config.teamspeak.address = jsonConfig.teamspeak.address;
|
||||
config.teamspeak.port = jsonConfig.teamspeak.port;
|
||||
config.teamspeak.serverPassword = jsonConfig.teamspeak.serverPassword;
|
||||
config.teamspeak.nickname = jsonConfig.teamspeak.nickname;
|
||||
config.teamspeak.defaultChannelId =
|
||||
jsonConfig.teamspeak.defaultChannelId;
|
||||
config.teamspeak.channelPassword = jsonConfig.teamspeak.channelPassword;
|
||||
config.teamspeak.adminGroups = jsonConfig.teamspeak.adminGroups;
|
||||
config.teamspeak.djGroups = jsonConfig.teamspeak.djGroups;
|
||||
config.teamspeak.bannedGroups = jsonConfig.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
config.security.requireAuthForAdmin =
|
||||
jsonConfig.security.requireAuthForAdmin;
|
||||
config.security.maxLoginAttempts = jsonConfig.security.maxLoginAttempts;
|
||||
config.security.lockoutDuration =
|
||||
std::chrono::milliseconds(jsonConfig.security.lockoutDuration);
|
||||
config.security.sessionTimeout =
|
||||
std::chrono::milliseconds(jsonConfig.security.sessionTimeout);
|
||||
config.security.enableAuditLog = jsonConfig.security.enableAuditLog;
|
||||
if (jsonConfig.security.apiKey.has_value()) {
|
||||
config.security.apiKey = jsonConfig.security.apiKey;
|
||||
}
|
||||
config.security.allowCors = jsonConfig.security.allowCors;
|
||||
config.security.corsOrigins = jsonConfig.security.corsOrigins;
|
||||
|
||||
// Features config
|
||||
config.features.enableHotReload = jsonConfig.features.enableHotReload;
|
||||
config.features.enableAutoReload = jsonConfig.features.enableAutoReload;
|
||||
config.features.enableAutoUpdateCheck =
|
||||
jsonConfig.features.enableAutoUpdateCheck;
|
||||
config.features.autoStartOnSubscribe =
|
||||
jsonConfig.features.autoStartOnSubscribe;
|
||||
config.features.pauseOnEmptySubscribers =
|
||||
jsonConfig.features.pauseOnEmptySubscribers;
|
||||
config.features.resumeOnSubscriberReturn =
|
||||
jsonConfig.features.resumeOnSubscriberReturn;
|
||||
config.features.shuffleMode = jsonConfig.features.shuffleMode;
|
||||
config.features.repeatMode = jsonConfig.features.repeatMode;
|
||||
config.features.maxHistorySize = jsonConfig.features.maxHistorySize;
|
||||
|
||||
// Advanced config
|
||||
config.advanced.sampleRate = jsonConfig.advanced.sampleRate;
|
||||
config.advanced.channels = jsonConfig.advanced.channels;
|
||||
config.advanced.bufferSize = jsonConfig.advanced.bufferSize;
|
||||
config.advanced.commandThreadPoolSize =
|
||||
jsonConfig.advanced.commandThreadPoolSize;
|
||||
config.advanced.networkThreadPoolSize =
|
||||
jsonConfig.advanced.networkThreadPoolSize;
|
||||
config.advanced.decoderThreads = jsonConfig.advanced.decoderThreads;
|
||||
config.advanced.cacheEnabled = jsonConfig.advanced.cacheEnabled;
|
||||
config.advanced.cacheMaxSize = jsonConfig.advanced.cacheMaxSize;
|
||||
config.advanced.cacheTTL = jsonConfig.advanced.cacheTTL;
|
||||
config.advanced.tempCleanupInterval =
|
||||
jsonConfig.advanced.tempCleanupInterval;
|
||||
|
||||
std::cout << std::format("[INFO] Configuration loaded from {}",
|
||||
path.string())
|
||||
<< std::endl;
|
||||
return config;
|
||||
}
|
||||
|
||||
Api::Result<void>
|
||||
ConfigLoader::saveToFile(const std::filesystem::path& path,
|
||||
const Api::AppConfig& config)
|
||||
{
|
||||
AppConfigJson jsonConfig;
|
||||
|
||||
// Bot config
|
||||
jsonConfig.bot.botName = config.bot.botName;
|
||||
jsonConfig.bot.commandPrefix = config.bot.commandPrefix;
|
||||
jsonConfig.bot.defaultVolume = config.bot.defaultVolume;
|
||||
jsonConfig.bot.maxQueueSize = config.bot.maxQueueSize;
|
||||
jsonConfig.bot.trackTimeout =
|
||||
static_cast<uint32_t>(config.bot.trackTimeout.count());
|
||||
jsonConfig.bot.autoReconnect = config.bot.autoReconnect;
|
||||
jsonConfig.bot.reconnectDelay =
|
||||
static_cast<uint32_t>(config.bot.reconnectDelay.count());
|
||||
jsonConfig.bot.maxReconnectAttempts = config.bot.maxReconnectAttempts;
|
||||
jsonConfig.bot.preferredQuality =
|
||||
qualityToString(config.bot.preferredQuality);
|
||||
jsonConfig.bot.allowPlaylistImports = config.bot.allowPlaylistImports;
|
||||
|
||||
// YouTube config
|
||||
if (config.youtube.youtubeApiKey.has_value()) {
|
||||
jsonConfig.youtube.youtubeApiKey = *config.youtube.youtubeApiKey;
|
||||
}
|
||||
if (config.youtube.youtubeCookiesPath.has_value()) {
|
||||
jsonConfig.youtube.youtubeCookiesPath =
|
||||
*config.youtube.youtubeCookiesPath;
|
||||
}
|
||||
if (config.youtube.cookiesFromBrowser.has_value()) {
|
||||
jsonConfig.youtube.cookiesFromBrowser =
|
||||
*config.youtube.cookiesFromBrowser;
|
||||
}
|
||||
jsonConfig.youtube.ytDlpMaxConcurrent =
|
||||
static_cast<uint32_t>(config.youtube.ytDlpMaxConcurrent);
|
||||
jsonConfig.youtube.ytDlpAsyncEnabled = config.youtube.ytDlpAsyncEnabled;
|
||||
jsonConfig.youtube.enableUrlShortening =
|
||||
config.youtube.enableUrlShortening;
|
||||
|
||||
// Network config
|
||||
if (config.network.proxy.has_value()) {
|
||||
jsonConfig.network.proxyHost = config.network.proxy->host;
|
||||
jsonConfig.network.proxyPort = config.network.proxy->port;
|
||||
jsonConfig.network.proxyUsername = config.network.proxy->username;
|
||||
jsonConfig.network.proxyPassword = config.network.proxy->password;
|
||||
jsonConfig.network.proxyType =
|
||||
config.network.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5"
|
||||
: "HTTP";
|
||||
}
|
||||
jsonConfig.network.rpcEnabled = config.network.rpcEnabled;
|
||||
jsonConfig.network.rpcPort = config.network.rpcPort;
|
||||
jsonConfig.network.rpcBindAddress = config.network.rpcBindAddress;
|
||||
|
||||
// TeamSpeak config
|
||||
jsonConfig.teamspeak.address = config.teamspeak.address;
|
||||
jsonConfig.teamspeak.port = config.teamspeak.port;
|
||||
jsonConfig.teamspeak.serverPassword = config.teamspeak.serverPassword;
|
||||
jsonConfig.teamspeak.nickname = config.teamspeak.nickname;
|
||||
jsonConfig.teamspeak.defaultChannelId =
|
||||
config.teamspeak.defaultChannelId;
|
||||
jsonConfig.teamspeak.channelPassword = config.teamspeak.channelPassword;
|
||||
jsonConfig.teamspeak.adminGroups = config.teamspeak.adminGroups;
|
||||
jsonConfig.teamspeak.djGroups = config.teamspeak.djGroups;
|
||||
jsonConfig.teamspeak.bannedGroups = config.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
jsonConfig.security.requireAuthForAdmin =
|
||||
config.security.requireAuthForAdmin;
|
||||
jsonConfig.security.maxLoginAttempts = config.security.maxLoginAttempts;
|
||||
jsonConfig.security.lockoutDuration =
|
||||
static_cast<uint32_t>(config.security.lockoutDuration.count());
|
||||
jsonConfig.security.sessionTimeout =
|
||||
static_cast<uint32_t>(config.security.sessionTimeout.count());
|
||||
jsonConfig.security.enableAuditLog = config.security.enableAuditLog;
|
||||
if (config.security.apiKey.has_value()) {
|
||||
jsonConfig.security.apiKey = *config.security.apiKey;
|
||||
}
|
||||
jsonConfig.security.allowCors = config.security.allowCors;
|
||||
jsonConfig.security.corsOrigins = config.security.corsOrigins;
|
||||
|
||||
// Features config
|
||||
jsonConfig.features.enableHotReload = config.features.enableHotReload;
|
||||
jsonConfig.features.enableAutoReload = config.features.enableAutoReload;
|
||||
jsonConfig.features.enableAutoUpdateCheck =
|
||||
config.features.enableAutoUpdateCheck;
|
||||
jsonConfig.features.autoStartOnSubscribe =
|
||||
config.features.autoStartOnSubscribe;
|
||||
jsonConfig.features.pauseOnEmptySubscribers =
|
||||
config.features.pauseOnEmptySubscribers;
|
||||
jsonConfig.features.resumeOnSubscriberReturn =
|
||||
config.features.resumeOnSubscriberReturn;
|
||||
jsonConfig.features.shuffleMode = config.features.shuffleMode;
|
||||
jsonConfig.features.repeatMode = config.features.repeatMode;
|
||||
jsonConfig.features.maxHistorySize = config.features.maxHistorySize;
|
||||
|
||||
// Advanced config
|
||||
jsonConfig.advanced.sampleRate = config.advanced.sampleRate;
|
||||
jsonConfig.advanced.channels = config.advanced.channels;
|
||||
jsonConfig.advanced.bufferSize = config.advanced.bufferSize;
|
||||
jsonConfig.advanced.commandThreadPoolSize =
|
||||
config.advanced.commandThreadPoolSize;
|
||||
jsonConfig.advanced.networkThreadPoolSize =
|
||||
config.advanced.networkThreadPoolSize;
|
||||
jsonConfig.advanced.decoderThreads = config.advanced.decoderThreads;
|
||||
jsonConfig.advanced.cacheEnabled = config.advanced.cacheEnabled;
|
||||
jsonConfig.advanced.cacheMaxSize = config.advanced.cacheMaxSize;
|
||||
jsonConfig.advanced.cacheTTL = config.advanced.cacheTTL;
|
||||
jsonConfig.advanced.tempCleanupInterval =
|
||||
config.advanced.tempCleanupInterval;
|
||||
|
||||
const std::string json = Void::Json::serialize(
|
||||
Void::Json::to_json(jsonConfig), {.pretty = true});
|
||||
std::ofstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::PermissionDenied,
|
||||
std::format("Failed to write configuration file: {}",
|
||||
path.string())));
|
||||
}
|
||||
|
||||
file << json;
|
||||
file.close();
|
||||
|
||||
ECHOION_LOG_INFO(
|
||||
std::format("Configuration saved to {}", path.string()));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::filesystem::path ConfigLoader::getDefaultConfigPath()
|
||||
{
|
||||
// Try to get config path from environment or use default
|
||||
const char* configHome = std::getenv("XDG_CONFIG_HOME");
|
||||
if (configHome) {
|
||||
return std::filesystem::path(configHome) / "echoion" /
|
||||
"config.json";
|
||||
}
|
||||
|
||||
const char* home = std::getenv("HOME");
|
||||
if (home) {
|
||||
return std::filesystem::path(home) / ".config" / "echoion" /
|
||||
"config.json";
|
||||
}
|
||||
|
||||
// Fallback to current directory
|
||||
return "echoion_config.json";
|
||||
}
|
||||
|
||||
Api::AppConfig ConfigLoader::createDefaultConfig()
|
||||
{
|
||||
Api::AppConfig config;
|
||||
|
||||
// Default bot config
|
||||
config.bot.botName = "Echoion";
|
||||
config.bot.commandPrefix = "!";
|
||||
config.bot.defaultVolume = 0.5f;
|
||||
config.bot.maxQueueSize = 100;
|
||||
config.bot.trackTimeout = std::chrono::milliseconds(30000);
|
||||
config.bot.autoReconnect = true;
|
||||
config.bot.reconnectDelay = std::chrono::milliseconds(5000);
|
||||
config.bot.maxReconnectAttempts = 5;
|
||||
config.bot.preferredQuality = Api::AudioQuality::High;
|
||||
config.bot.allowPlaylistImports = true;
|
||||
|
||||
// Default YouTube config
|
||||
config.youtube.ytDlpMaxConcurrent = 4;
|
||||
config.youtube.ytDlpAsyncEnabled = true;
|
||||
config.youtube.enableUrlShortening = true;
|
||||
|
||||
// Default network config
|
||||
config.network.rpcEnabled = true;
|
||||
config.network.rpcPort = 8765;
|
||||
config.network.rpcBindAddress = "127.0.0.1";
|
||||
|
||||
// Default TeamSpeak config
|
||||
config.teamspeak.address = "localhost";
|
||||
config.teamspeak.port = 9987;
|
||||
config.teamspeak.nickname = "EchoionBot";
|
||||
|
||||
// Default security config
|
||||
config.security.requireAuthForAdmin = true;
|
||||
config.security.maxLoginAttempts = 5;
|
||||
config.security.lockoutDuration = std::chrono::milliseconds(300000);
|
||||
config.security.sessionTimeout = std::chrono::milliseconds(3600000);
|
||||
config.security.enableAuditLog = true;
|
||||
config.security.allowCors = false;
|
||||
|
||||
// Default features config
|
||||
config.features.enableHotReload = true;
|
||||
config.features.enableAutoReload = false;
|
||||
config.features.enableAutoUpdateCheck = true;
|
||||
config.features.autoStartOnSubscribe = true;
|
||||
config.features.pauseOnEmptySubscribers = true;
|
||||
config.features.resumeOnSubscriberReturn = true;
|
||||
config.features.shuffleMode = "random";
|
||||
config.features.repeatMode = "off";
|
||||
config.features.maxHistorySize = 50;
|
||||
|
||||
// Default advanced config
|
||||
config.advanced.sampleRate = 192000;
|
||||
config.advanced.channels = 2;
|
||||
config.advanced.bufferSize = 7680;
|
||||
config.advanced.commandThreadPoolSize = 4;
|
||||
config.advanced.networkThreadPoolSize = 4;
|
||||
config.advanced.decoderThreads = 2;
|
||||
config.advanced.cacheEnabled = true;
|
||||
config.advanced.cacheMaxSize = "1GB";
|
||||
config.advanced.cacheTTL = 86400;
|
||||
config.advanced.tempCleanupInterval = 3600;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
Api::AudioQuality ConfigLoader::parseQuality(const std::string& str)
|
||||
{
|
||||
if (str == "Low") {
|
||||
return Api::AudioQuality::Low;
|
||||
}
|
||||
if (str == "Medium") {
|
||||
return Api::AudioQuality::Medium;
|
||||
}
|
||||
if (str == "High") {
|
||||
return Api::AudioQuality::High;
|
||||
}
|
||||
if (str == "VeryHigh") {
|
||||
return Api::AudioQuality::VeryHigh;
|
||||
}
|
||||
if (str == "Lossless") {
|
||||
return Api::AudioQuality::Lossless;
|
||||
}
|
||||
return Api::AudioQuality::High; // Default
|
||||
}
|
||||
|
||||
std::string ConfigLoader::qualityToString(Api::AudioQuality quality)
|
||||
{
|
||||
switch (quality) {
|
||||
case Api::AudioQuality::Low:
|
||||
return "Low";
|
||||
case Api::AudioQuality::Medium:
|
||||
return "Medium";
|
||||
case Api::AudioQuality::High:
|
||||
return "High";
|
||||
case Api::AudioQuality::VeryHigh:
|
||||
return "VeryHigh";
|
||||
case Api::AudioQuality::Lossless:
|
||||
return "Lossless";
|
||||
default:
|
||||
return "High";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user