Serialize AppConfig directly via Void::Json reflection
Drop the parallel *Json mirror structs and the ~370 LOC of field-by-field
mapping in ConfigLoader and Core. Api::AppConfig is now reflected straight
to/from JSON:
- ConfigLoader::{loadFromFile,saveToFile} use from_json/to_json<AppConfig>
directly; a small normalize() keeps the yt-dlp concurrency cap that the
mapping layer used to enforce.
- Core::{getConfigJson,updateConfigJson} likewise reflect directly, which
also extends them to honor youtube/features/advanced sections that the
old hand-mapping silently dropped.
- parseQuality/qualityToString removed — enums serialize by name.
On-disk format change (v3, clean cutover): durations are strings
("30000ms", also accepts "30s"/"5m"/"1h"), enums are names, the proxy is a
nested object (or null) instead of flat proxyType/proxyHost fields, and the
admin/custom sections now round-trip. Verified: build green, a
serialize->parse->deserialize round-trip of AppConfig (incl. a SOCKS5
proxy) passes, and config.example.json loads through the reflection path.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9ab0d6b75
commit
51f1651c18
3 files changed
+31
-477
No files matched your search
@@ -8,101 +8,12 @@
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// JSON structures for configuration (public for use by Core)
|
||||
struct BotConfigJson {
|
||||
std::string botName = "Echoion";
|
||||
std::string commandPrefix = "!";
|
||||
float defaultVolume = 0.5f;
|
||||
uint32_t maxQueueSize = 100;
|
||||
uint32_t trackTimeout = 30000;
|
||||
bool autoReconnect = true;
|
||||
uint32_t reconnectDelay = 5000;
|
||||
uint32_t maxReconnectAttempts = 5;
|
||||
std::string preferredQuality = "High";
|
||||
bool allowPlaylistImports = true;
|
||||
};
|
||||
|
||||
struct YouTubeConfigJson {
|
||||
std::optional<std::string> youtubeApiKey;
|
||||
std::optional<std::string> youtubeCookiesPath;
|
||||
std::optional<std::string> cookiesFromBrowser;
|
||||
uint32_t ytDlpMaxConcurrent = 4;
|
||||
bool ytDlpAsyncEnabled = true;
|
||||
bool enableUrlShortening = true;
|
||||
};
|
||||
|
||||
struct NetworkConfigJson {
|
||||
// Proxy settings (flat structure for backward compatibility)
|
||||
std::string proxyType = "HTTP";
|
||||
std::optional<std::string> proxyHost;
|
||||
std::optional<uint16_t> proxyPort;
|
||||
std::optional<std::string> proxyUsername;
|
||||
std::optional<std::string> proxyPassword;
|
||||
// RPC settings
|
||||
bool rpcEnabled = true;
|
||||
uint16_t rpcPort = 8765;
|
||||
std::string rpcBindAddress = "127.0.0.1";
|
||||
};
|
||||
|
||||
struct TeamSpeakConfigJson {
|
||||
std::string address = "localhost";
|
||||
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;
|
||||
};
|
||||
|
||||
struct SecurityConfigJson {
|
||||
bool requireAuthForAdmin = true;
|
||||
uint32_t maxLoginAttempts = 5;
|
||||
uint32_t lockoutDuration = 300000;
|
||||
uint32_t sessionTimeout = 3600000;
|
||||
bool enableAuditLog = true;
|
||||
std::optional<std::string> apiKey;
|
||||
bool allowCors = false;
|
||||
std::vector<std::string> corsOrigins;
|
||||
};
|
||||
|
||||
struct FeaturesConfigJson {
|
||||
bool enableHotReload = true;
|
||||
bool enableAutoReload = false;
|
||||
bool enableAutoUpdateCheck = true;
|
||||
bool autoStartOnSubscribe = true;
|
||||
bool pauseOnEmptySubscribers = true;
|
||||
bool resumeOnSubscriberReturn = true;
|
||||
std::string shuffleMode = "random";
|
||||
std::string repeatMode = "off";
|
||||
uint32_t maxHistorySize = 50;
|
||||
};
|
||||
|
||||
struct AdvancedConfigJson {
|
||||
uint32_t sampleRate = 192000;
|
||||
uint32_t channels = 2;
|
||||
uint32_t bufferSize = 7680;
|
||||
uint32_t commandThreadPoolSize = 4;
|
||||
uint32_t networkThreadPoolSize = 4;
|
||||
uint32_t decoderThreads = 2;
|
||||
bool cacheEnabled = true;
|
||||
std::string cacheMaxSize = "1GB";
|
||||
uint32_t cacheTTL = 86400;
|
||||
uint32_t tempCleanupInterval = 3600;
|
||||
};
|
||||
|
||||
struct AppConfigJson {
|
||||
BotConfigJson bot;
|
||||
YouTubeConfigJson youtube;
|
||||
NetworkConfigJson network;
|
||||
TeamSpeakConfigJson teamspeak;
|
||||
SecurityConfigJson security;
|
||||
FeaturesConfigJson features;
|
||||
AdvancedConfigJson advanced;
|
||||
};
|
||||
|
||||
// Configuration loader for JSON config files (internal to Core)
|
||||
// Configuration loader for JSON config files (internal to Core).
|
||||
//
|
||||
// Api::AppConfig is serialized directly through Void::Json reflection —
|
||||
// no mirror DTOs. The on-disk shape follows the domain types: durations
|
||||
// are strings ("30000ms"), enums are names ("High"), the proxy is a
|
||||
// nested object (or null), and time_points are duration strings.
|
||||
class ConfigLoader
|
||||
{
|
||||
public:
|
||||
@@ -122,11 +33,9 @@ namespace Echoion::Core
|
||||
// Create default configuration
|
||||
[[nodiscard]] static Api::AppConfig createDefaultConfig();
|
||||
|
||||
// Parse quality string to enum
|
||||
[[nodiscard]] static Api::AudioQuality
|
||||
parseQuality(const std::string& str);
|
||||
[[nodiscard]] static std::string
|
||||
qualityToString(Api::AudioQuality quality);
|
||||
// Clamp/normalize values that carry hard limits after loading
|
||||
// (e.g. the yt-dlp concurrency cap). Applied by loadFromFile.
|
||||
static void normalize(Api::AppConfig& config);
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
+16
-271
@@ -8,14 +8,16 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
// 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.
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
void ConfigLoader::normalize(Api::AppConfig& config)
|
||||
{
|
||||
// yt-dlp concurrency is hard-capped regardless of what the file says.
|
||||
config.youtube.ytDlpMaxConcurrent =
|
||||
std::min(config.youtube.ytDlpMaxConcurrent, size_t(8));
|
||||
}
|
||||
|
||||
Api::Result<Api::AppConfig>
|
||||
ConfigLoader::loadFromFile(const std::filesystem::path& path)
|
||||
{
|
||||
@@ -45,132 +47,19 @@ namespace Echoion::Core
|
||||
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) {
|
||||
// Deserialize Api::AppConfig directly via reflection: unknown keys are
|
||||
// ignored and absent fields keep their defaults. Durations are duration
|
||||
// strings ("30000ms"), enums are names, the proxy is a nested object.
|
||||
auto result = Void::Json::from_json<Api::AppConfig>(*parsed);
|
||||
if (!result) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("Failed to parse configuration: field '{}': {}",
|
||||
jsonResult.error().field,
|
||||
jsonResult.error().message));
|
||||
result.error().field, result.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;
|
||||
Api::AppConfig config = std::move(*result);
|
||||
normalize(config);
|
||||
|
||||
std::cout << std::format("[INFO] Configuration loaded from {}",
|
||||
path.string())
|
||||
@@ -182,114 +71,8 @@ namespace Echoion::Core
|
||||
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});
|
||||
Void::Json::to_json(config), {.pretty = true});
|
||||
std::ofstream file(path);
|
||||
if (!file.is_open()) {
|
||||
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||
@@ -389,42 +172,4 @@ namespace Echoion::Core
|
||||
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
|
||||
+6
-106
@@ -854,7 +854,8 @@ namespace Echoion::Core
|
||||
parsed.error().line, parsed.error().col,
|
||||
parsed.error().message));
|
||||
}
|
||||
auto jsonResult = Void::Json::from_json<AppConfigJson>(*parsed);
|
||||
// Deserialize the full Api::AppConfig document directly via reflection.
|
||||
auto jsonResult = Void::Json::from_json<Api::AppConfig>(*parsed);
|
||||
if (!jsonResult.has_value()) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ParseError,
|
||||
@@ -863,116 +864,15 @@ namespace Echoion::Core
|
||||
jsonResult.error().message));
|
||||
}
|
||||
|
||||
// Convert to AppConfig
|
||||
Api::AppConfig config;
|
||||
const auto& json = jsonResult.value();
|
||||
|
||||
// Bot config
|
||||
config.bot.botName = json.bot.botName;
|
||||
config.bot.commandPrefix = json.bot.commandPrefix;
|
||||
config.bot.defaultVolume = json.bot.defaultVolume;
|
||||
config.bot.autoReconnect = json.bot.autoReconnect;
|
||||
config.bot.reconnectDelay =
|
||||
std::chrono::milliseconds(json.bot.reconnectDelay);
|
||||
config.bot.maxReconnectAttempts = json.bot.maxReconnectAttempts;
|
||||
config.bot.preferredQuality =
|
||||
ConfigLoader::parseQuality(json.bot.preferredQuality);
|
||||
config.bot.allowPlaylistImports = json.bot.allowPlaylistImports;
|
||||
config.bot.maxQueueSize = json.bot.maxQueueSize;
|
||||
config.bot.trackTimeout =
|
||||
std::chrono::milliseconds(json.bot.trackTimeout);
|
||||
|
||||
// Proxy config
|
||||
if (json.network.proxyHost.has_value() &&
|
||||
json.network.proxyPort.has_value()) {
|
||||
Api::ProxyConfig proxy;
|
||||
proxy.host = *json.network.proxyHost;
|
||||
proxy.port = *json.network.proxyPort;
|
||||
proxy.username = json.network.proxyUsername;
|
||||
proxy.password = json.network.proxyPassword;
|
||||
proxy.type = (json.network.proxyType == "SOCKS5")
|
||||
? Api::ProxyType::SOCKS5
|
||||
: Api::ProxyType::HTTP;
|
||||
config.network.proxy = proxy;
|
||||
}
|
||||
|
||||
// Server config
|
||||
config.teamspeak.address = json.teamspeak.address;
|
||||
config.teamspeak.port = json.teamspeak.port;
|
||||
config.teamspeak.serverPassword = json.teamspeak.serverPassword;
|
||||
config.teamspeak.nickname = json.teamspeak.nickname;
|
||||
config.teamspeak.defaultChannelId = json.teamspeak.defaultChannelId;
|
||||
config.teamspeak.channelPassword = json.teamspeak.channelPassword;
|
||||
config.teamspeak.adminGroups = json.teamspeak.adminGroups;
|
||||
config.teamspeak.djGroups = json.teamspeak.djGroups;
|
||||
config.teamspeak.bannedGroups = json.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
config.security.requireAuthForAdmin = json.security.requireAuthForAdmin;
|
||||
config.security.maxLoginAttempts = json.security.maxLoginAttempts;
|
||||
config.security.lockoutDuration =
|
||||
std::chrono::milliseconds(json.security.lockoutDuration);
|
||||
config.security.sessionTimeout =
|
||||
std::chrono::milliseconds(json.security.sessionTimeout);
|
||||
config.security.enableAuditLog = json.security.enableAuditLog;
|
||||
|
||||
// Update the config
|
||||
Api::AppConfig config = std::move(jsonResult.value());
|
||||
ConfigLoader::normalize(config);
|
||||
return updateConfig(config);
|
||||
}
|
||||
|
||||
Api::Result<std::string> Core::getConfigJson() const
|
||||
{
|
||||
// Convert current config to JSON
|
||||
AppConfigJson json;
|
||||
|
||||
// Bot config
|
||||
json.bot.botName = config_.bot.botName;
|
||||
json.bot.commandPrefix = config_.bot.commandPrefix;
|
||||
json.bot.defaultVolume = config_.bot.defaultVolume;
|
||||
json.bot.autoReconnect = config_.bot.autoReconnect;
|
||||
json.bot.reconnectDelay =
|
||||
static_cast<uint32_t>(config_.bot.reconnectDelay.count());
|
||||
json.bot.maxReconnectAttempts = config_.bot.maxReconnectAttempts;
|
||||
json.bot.preferredQuality =
|
||||
ConfigLoader::qualityToString(config_.bot.preferredQuality);
|
||||
json.bot.allowPlaylistImports = config_.bot.allowPlaylistImports;
|
||||
json.bot.maxQueueSize = config_.bot.maxQueueSize;
|
||||
json.bot.trackTimeout =
|
||||
static_cast<uint32_t>(config_.bot.trackTimeout.count());
|
||||
|
||||
// Proxy config
|
||||
if (config_.network.proxy.has_value()) {
|
||||
json.network.proxyHost = config_.network.proxy->host;
|
||||
json.network.proxyPort = config_.network.proxy->port;
|
||||
json.network.proxyUsername = config_.network.proxy->username;
|
||||
json.network.proxyPassword = config_.network.proxy->password;
|
||||
json.network.proxyType =
|
||||
config_.network.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5"
|
||||
: "HTTP";
|
||||
}
|
||||
|
||||
// Server config
|
||||
json.teamspeak.address = config_.teamspeak.address;
|
||||
json.teamspeak.port = config_.teamspeak.port;
|
||||
json.teamspeak.serverPassword = config_.teamspeak.serverPassword;
|
||||
json.teamspeak.nickname = config_.teamspeak.nickname;
|
||||
json.teamspeak.defaultChannelId = config_.teamspeak.defaultChannelId;
|
||||
json.teamspeak.channelPassword = config_.teamspeak.channelPassword;
|
||||
json.teamspeak.adminGroups = config_.teamspeak.adminGroups;
|
||||
json.teamspeak.djGroups = config_.teamspeak.djGroups;
|
||||
json.teamspeak.bannedGroups = config_.teamspeak.bannedGroups;
|
||||
|
||||
// Security config
|
||||
json.security.requireAuthForAdmin =
|
||||
config_.security.requireAuthForAdmin;
|
||||
json.security.maxLoginAttempts = config_.security.maxLoginAttempts;
|
||||
json.security.lockoutDuration =
|
||||
static_cast<uint32_t>(config_.security.lockoutDuration.count());
|
||||
json.security.sessionTimeout =
|
||||
static_cast<uint32_t>(config_.security.sessionTimeout.count());
|
||||
json.security.enableAuditLog = config_.security.enableAuditLog;
|
||||
|
||||
return Void::Json::serialize(Void::Json::to_json(json),
|
||||
// Serialize the current config directly via reflection.
|
||||
return Void::Json::serialize(Void::Json::to_json(config_),
|
||||
{.pretty = true});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user