From 5c93d48bb82e49f02566b896213ea227c7b2421f Mon Sep 17 00:00:00 2001 From: Slavasil Date: Wed, 20 Nov 2024 14:09:47 +0300 Subject: [PATCH] add configuration --- CMakeLists.txt | 2 +- config.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 23 +++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 config.cpp create mode 100644 config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 906a0ce..f62ebd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ add_subdirectory(libuv) add_subdirectory(spdlog) add_subdirectory(td) -add_executable(${PROJECT_NAME} main.cpp http.cpp state.cpp vk.cpp) +add_executable(${PROJECT_NAME} main.cpp config.cpp http.cpp state.cpp vk.cpp) target_compile_options(${PROJECT_NAME} PRIVATE -std=c++2b) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..887264f --- /dev/null +++ b/config.cpp @@ -0,0 +1,101 @@ +#include "config.h" +#include "nlohmann/json.hpp" +#include +#include + +using namespace config; +using json = nlohmann::json; + +const char *JSON_KEY_VK_SERVICE_KEY = "vk_service_key"; +const char *JSON_KEY_TG_API_ID = "tg_api_id"; +const char *JSON_KEY_TG_API_HASH = "tg_api_hash"; +const char *JSON_KEY_TG_PHONE_NUMBER = "tg_phone_number"; +const char *JSON_KEY_VK_SOURCE = "vk_source"; +const char *JSON_KEY_TG_SOURCE_ID = "tg_source_id"; +const char *JSON_KEY_TG_DESTINATION_ID = "tg_destination_id"; + +const char *ERR_INVALID_TYPE_VK_SERVICE_KEY = "vk_service_key must be a string"; +const char *ERR_INVALID_TYPE_TG_API_ID = "tg_api_id must be an integer or a string"; +const char *ERR_INVALID_TYPE_TG_API_HASH = "tg_api_hash must be a string"; +const char *ERR_INVALID_TYPE_TG_PHONE_NUMBER = "tg_phone_number must be a string"; +const char *ERR_INVALID_TYPE_VK_SOURCE = "vk_source must be an integer or a string"; +const char *ERR_INVALID_TYPE_TG_SOURCE_ID = "tg_source_id must be an integer"; +const char *ERR_INVALID_TYPE_TG_DESTINATION_ID = "tg_destination_id must be an integer"; + +AppConfig::AppConfig(const std::string &filename) { + std::ifstream f(filename); + if (f.fail()) { + return; + } + json config = json::parse(f); + + if (config.contains(JSON_KEY_VK_SERVICE_KEY)) { + json vkServiceKey_ = config[JSON_KEY_VK_SERVICE_KEY]; + if (vkServiceKey_.type() == json::value_t::string) { + vkServiceKey = vkServiceKey_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_VK_SERVICE_KEY); + } + } + + if (config.contains(JSON_KEY_TG_API_ID)) { + json tgApiId_ = config[JSON_KEY_TG_API_ID]; + json::value_t t = tgApiId_.type(); + if (t == json::value_t::string) { + std::string tgApiIdStr = tgApiId_; + tgApiId = std::stol(tgApiIdStr.c_str()); + } else if (t == json::value_t::number_integer || t == json::value_t::number_unsigned) { + tgApiId = tgApiId_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_TG_API_ID); + } + } + + if (config.contains(JSON_KEY_TG_API_HASH)) { + json tgApiHash_ = config[JSON_KEY_TG_API_HASH]; + if (tgApiHash_.type() == json::value_t::string) { + tgApiHash = tgApiHash_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_TG_API_HASH); + } + } + + if (config.contains(JSON_KEY_TG_PHONE_NUMBER)) { + json tgPhoneNumber_ = config[JSON_KEY_TG_PHONE_NUMBER]; + if (tgPhoneNumber_.type() == json::value_t::string) { + tgPhoneNumber = tgPhoneNumber_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_TG_PHONE_NUMBER); + } + } + + if (config.contains(JSON_KEY_VK_SOURCE)) { + json vkSource_ = config[JSON_KEY_VK_SOURCE]; + json::value_t t = vkSource_.type(); + if (t == json::value_t::string) { + vkSource = vkSource_.get(); + } else if (t == json::value_t::number_integer || t == json::value_t::number_unsigned) { + vkSource = vkSource_.get(); + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCE); + } + } + + if (config.contains(JSON_KEY_TG_SOURCE_ID)) { + json tgSourceId_ = config[JSON_KEY_TG_SOURCE_ID]; + if (tgSourceId_.type() == json::value_t::number_integer || tgSourceId_.type() == json::value_t::number_unsigned) { + tgSourceId = tgSourceId_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_TG_SOURCE_ID); + } + } + + if (config.contains(JSON_KEY_TG_DESTINATION_ID)) { + json tgDestinationId_ = config[JSON_KEY_TG_DESTINATION_ID]; + if (tgDestinationId_.type() == json::value_t::number_integer || tgDestinationId_.type() == json::value_t::number_unsigned) { + tgDestinationId = tgDestinationId_; + } else { + throw InvalidConfigException(ERR_INVALID_TYPE_TG_DESTINATION_ID); + } + } +} \ No newline at end of file diff --git a/config.h b/config.h new file mode 100644 index 0000000..3a245c6 --- /dev/null +++ b/config.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +namespace config { + struct InvalidConfigException : std::exception { + inline InvalidConfigException(const char *message) : message(message) {}; + const char *message; + }; + + struct AppConfig { + AppConfig(const std::string &filename); + + std::string vkServiceKey; + long tgApiId; + std::string tgApiHash; + std::string tgPhoneNumber; + std::variant vkSource; + long tgSourceId, tgDestinationId; + }; +} \ No newline at end of file