add configuration

This commit is contained in:
Slavasil 2024-11-20 14:09:47 +03:00
parent 57d59399cd
commit 5c93d48bb8
3 changed files with 125 additions and 1 deletions

View File

@ -7,7 +7,7 @@ add_subdirectory(libuv)
add_subdirectory(spdlog) add_subdirectory(spdlog)
add_subdirectory(td) 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) target_compile_options(${PROJECT_NAME} PRIVATE -std=c++2b)

101
config.cpp Normal file
View File

@ -0,0 +1,101 @@
#include "config.h"
#include "nlohmann/json.hpp"
#include <fstream>
#include <string>
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<std::string>();
} else if (t == json::value_t::number_integer || t == json::value_t::number_unsigned) {
vkSource = vkSource_.get<long>();
} 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);
}
}
}

23
config.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <exception>
#include <string>
#include <variant>
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<long, std::string> vkSource;
long tgSourceId, tgDestinationId;
};
}