Добавил C++-сервер
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# Модуль для работы с конфигурационными файлами
|
||||
project(la_configuration)
|
||||
|
||||
# Собираем исходные файлы
|
||||
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
|
||||
|
||||
# Создаём библиотеку
|
||||
add_library(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
# Указываем директории с заголовочными файлами
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
# Подключаем зависимости
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
la_json
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
/// @brief Парсинг конфигурационного файла
|
||||
/// @param[in] path_to_configuration Путь к конфигурационному файлу
|
||||
/// @param[in] required_keys Список обязательных ключей в формате { "key1": ["subkey1", ...], "key2": ... }
|
||||
/// @returns Конфигурация в формате json
|
||||
json ParseConfiguration(const std::string& path_to_configuration, const json& required_keys);
|
||||
@@ -0,0 +1,16 @@
|
||||
# Модуль configuration
|
||||
Конфигурационный файл должен иметь следующий формат:
|
||||
```json
|
||||
{
|
||||
"key1": {
|
||||
"subkey1": "value1",
|
||||
"subkey2": "value2",
|
||||
...
|
||||
},
|
||||
"key2": {
|
||||
"subkey1": "value1",
|
||||
...
|
||||
},
|
||||
...
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <configuration/configuration.h>
|
||||
|
||||
json ParseConfiguration(const std::string& path_to_configuration, const json& required_keys) {
|
||||
std::ifstream input(path_to_configuration);
|
||||
auto configuration = json::parse(input);
|
||||
|
||||
for (const auto& [key, value] : required_keys.items()) {
|
||||
// Проверяем наличие ключа на "нулевом" уровне
|
||||
if (!configuration.contains(key)) {
|
||||
std::ostringstream message;
|
||||
message << "configuration doesn't contain required key '" << key << "'";
|
||||
throw std::runtime_error(message.str());
|
||||
}
|
||||
|
||||
// Проверяем наличие вложенных ключей
|
||||
auto configuration_value = configuration[key];
|
||||
|
||||
for (const auto& item : value) {
|
||||
if (!configuration_value.contains(item)) {
|
||||
std::ostringstream message;
|
||||
message << "configuration doesn't contain required key '" << item << "' in '" << key << "'";
|
||||
throw std::runtime_error(message.str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
Reference in New Issue
Block a user