pixwhile/config.cpp

55 lines
1.8 KiB
C++
Raw Normal View History

2023-04-03 08:57:51 +00:00
#include <stdexcept>
#include "file.h"
#include "config.h"
Config load_config(const char* path) {
File config_file(path, "r");
return nlohmann::json::parse(config_file.get());
2023-04-03 08:57:51 +00:00
}
2023-06-08 16:02:36 +00:00
RedisConfig get_redis_config(const nlohmann::json& j) {
using namespace std::string_literals;
RedisConfig redis_config;
const nlohmann::json& connection = j.at("connection");
const std::string& connection_type = connection.at("type").get_ref<const std::string&>();
if (connection_type == "ip") {
redis_config.connection_method = IPConnection{
connection.at("address").get<std::string>(),
connection.at("port").get<int>()
};
} else if (connection_type == "unix") {
redis_config.connection_method = UnixConnection{connection.at("unix").get<std::string>()};
} else {
throw std::invalid_argument("Unknown redis connection type: "s + connection_type);
}
if (j.at("username").is_string()) {
redis_config.username = j["username"].get<std::string>();
}
if (j.at("password").is_string()) {
redis_config.password = j["password"].get<std::string>();
}
return redis_config;
}
2023-04-03 08:57:51 +00:00
void from_json(const nlohmann::json& j, Config& config) {
using namespace std::string_literals;
j.at("bind_host").get_to(config.bind_host);
j.at("bind_port").get_to(config.bind_port);
if (config.bind_port < 0) {
throw std::invalid_argument("Invalid port to bind to: "s + std::to_string(config.bind_port));
}
2023-04-06 12:24:09 +00:00
config.image_proxy_url = j.at("image_proxy_url").get<std::string>();
2023-06-04 05:08:49 +00:00
if (j.contains("canonical_origin") && j["canonical_origin"].is_string()) {
config.canonical_origin = j["canonical_origin"].get<std::string>();
}
2023-06-08 16:02:36 +00:00
if (j.contains("redis") && j["redis"].at("enabled")) {
config.redis_config = get_redis_config(j["redis"]);
}
2023-04-03 08:57:51 +00:00
}