Add configuration from toml file
This commit is contained in:
97
src/config.cpp
Normal file
97
src/config.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "config.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <toml.hpp>
|
||||
|
||||
namespace weaseldb {
|
||||
|
||||
std::optional<Config>
|
||||
ConfigParser::load_from_file(const std::string &file_path) {
|
||||
try {
|
||||
const auto toml_data = toml::parse(file_path);
|
||||
Config config;
|
||||
|
||||
parse_server_config(toml_data, config.server);
|
||||
parse_commit_config(toml_data, config.commit);
|
||||
parse_subscription_config(toml_data, config.subscription);
|
||||
|
||||
return config;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Error parsing config file '" << file_path << "': " << e.what()
|
||||
<< std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<Config>
|
||||
ConfigParser::parse_toml_string(const std::string &toml_content) {
|
||||
try {
|
||||
const auto toml_data = toml::parse_str(toml_content);
|
||||
Config config;
|
||||
|
||||
parse_server_config(toml_data, config.server);
|
||||
parse_commit_config(toml_data, config.commit);
|
||||
parse_subscription_config(toml_data, config.subscription);
|
||||
|
||||
return config;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Error parsing TOML content: " << e.what() << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigParser::parse_server_config(const auto &toml_data,
|
||||
ServerConfig &config) {
|
||||
if (toml_data.contains("server")) {
|
||||
const auto &srv = toml_data.at("server");
|
||||
|
||||
if (srv.contains("bind_address")) {
|
||||
config.bind_address = toml::get<std::string>(srv.at("bind_address"));
|
||||
}
|
||||
if (srv.contains("port")) {
|
||||
config.port = toml::get<int>(srv.at("port"));
|
||||
}
|
||||
if (srv.contains("max_request_size_bytes")) {
|
||||
config.max_request_size_bytes =
|
||||
toml::get<size_t>(srv.at("max_request_size_bytes"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigParser::parse_commit_config(const auto &toml_data,
|
||||
CommitConfig &config) {
|
||||
if (toml_data.contains("commit")) {
|
||||
const auto &commit = toml_data.at("commit");
|
||||
|
||||
if (commit.contains("min_request_id_length")) {
|
||||
config.min_request_id_length =
|
||||
toml::get<size_t>(commit.at("min_request_id_length"));
|
||||
}
|
||||
if (commit.contains("request_id_retention_hours")) {
|
||||
auto hours = toml::get<int>(commit.at("request_id_retention_hours"));
|
||||
config.request_id_retention_time = std::chrono::hours{hours};
|
||||
}
|
||||
if (commit.contains("request_id_retention_versions")) {
|
||||
config.request_id_retention_versions =
|
||||
toml::get<size_t>(commit.at("request_id_retention_versions"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigParser::parse_subscription_config(const auto &toml_data,
|
||||
SubscriptionConfig &config) {
|
||||
if (toml_data.contains("subscription")) {
|
||||
const auto &sub = toml_data.at("subscription");
|
||||
|
||||
if (sub.contains("max_buffer_size_bytes")) {
|
||||
config.max_buffer_size_bytes =
|
||||
toml::get<size_t>(sub.at("max_buffer_size_bytes"));
|
||||
}
|
||||
if (sub.contains("keepalive_interval_seconds")) {
|
||||
auto seconds = toml::get<int>(sub.at("keepalive_interval_seconds"));
|
||||
config.keepalive_interval = std::chrono::seconds{seconds};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace weaseldb
|
||||
Reference in New Issue
Block a user