Compare commits

...

3 Commits

Author SHA1 Message Date
242109cfe5 fixes
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
2025-03-03 17:19:56 -06:00
0ffa461f91 read/write ui.json 2025-03-03 15:02:08 -06:00
ada11d6e62 read/write ui.json 2025-03-03 15:01:55 -06:00
3 changed files with 121 additions and 0 deletions

View File

@ -497,6 +497,7 @@ inline constexpr const auto JSON_MAX_UPLOAD_COUNT{"MaxUploadCount"};
inline constexpr const auto JSON_MED_FREQ_INTERVAL_SECS{
"MedFreqIntervalSeconds"};
inline constexpr const auto JSON_META{"Meta"};
inline constexpr const auto JSON_MOUNT_LOCATIONS{"MountLocations"};
inline constexpr const auto JSON_ONLINE_CHECK_RETRY_SECS{
"OnlineCheckRetrySeconds"};
inline constexpr const auto JSON_PATH{"Path"};

View File

@ -26,6 +26,9 @@
namespace repertory::ui {
class mgmt_app_config final {
public:
mgmt_app_config();
private:
atomic<std::string> api_auth_{"test"};
std::atomic<std::uint16_t> api_port_{default_ui_mgmt_port};
@ -35,6 +38,9 @@ private:
locations_;
mutable std::recursive_mutex mtx_;
private:
void save() const;
public:
[[nodiscard]] auto get_api_auth() const -> std::string { return api_auth_; }

View File

@ -21,7 +21,78 @@
*/
#include "ui/mgmt_app_config.hpp"
#include "app_config.hpp"
#include "utils/error_utils.hpp"
#include "utils/file.hpp"
#include "utils/path.hpp"
#include "utils/unix.hpp"
#include "utils/windows.hpp"
namespace {
[[nodiscard]] auto from_json(const nlohmann::json &json)
-> std::unordered_map<repertory::provider_type,
std::unordered_map<std::string, std::string>> {
std::unordered_map<repertory::provider_type,
std::unordered_map<std::string, std::string>>
map_of_maps{
{repertory::provider_type::encrypt, {}},
{repertory::provider_type::remote, {}},
{repertory::provider_type::s3, {}},
{repertory::provider_type::sia, {}},
};
for (const auto &[prov, map] : map_of_maps) {
for (const auto &[key, value] :
json[repertory::provider_type_to_string(prov)].items()) {
if (value.is_null()) {
continue;
}
map_of_maps[prov][key] = value;
}
}
return map_of_maps;
}
[[nodiscard]] auto to_json(const auto &map_of_maps) -> nlohmann::json {
nlohmann::json json;
for (const auto &[prov, map] : map_of_maps) {
for (const auto &[key, value] : map) {
json[repertory::provider_type_to_string(prov)][key] = value;
}
}
return json;
}
} // namespace
namespace repertory::ui {
mgmt_app_config::mgmt_app_config() {
REPERTORY_USES_FUNCTION_NAME();
auto config_file =
utils::path::combine(app_config::get_root_data_directory(), {"ui.json"});
try {
nlohmann::json data;
if (utils::file::read_json_file(config_file, data)) {
api_auth_ = data.at(JSON_API_AUTH).get<std::string>();
api_port_ = data.at(JSON_API_PORT).get<std::uint16_t>();
api_user_ = data.at(JSON_API_USER).get<std::string>();
locations_ = from_json(data.at(JSON_MOUNT_LOCATIONS));
return;
}
utils::error::raise_error(
function_name, utils::get_last_error_code(),
fmt::format("failed to read file|{}", config_file));
} catch (const std::exception &ex) {
utils::error::raise_error(
function_name, ex, fmt::format("failed to read file|{}", config_file));
}
}
auto mgmt_app_config::get_mount_location(provider_type prov,
std::string_view name) const
-> std::string {
@ -34,14 +105,57 @@ auto mgmt_app_config::get_mount_location(provider_type prov,
return "";
}
void mgmt_app_config::save() const {
REPERTORY_USES_FUNCTION_NAME();
auto config_file =
utils::path::combine(app_config::get_root_data_directory(), {"ui.json"});
try {
if (not utils::file::directory{app_config::get_root_data_directory()}
.create_directory()) {
utils::error::raise_error(
function_name, fmt::format("failed to create directory|{}",
app_config::get_root_data_directory()));
return;
}
nlohmann::json data;
data[JSON_API_AUTH] = api_auth_;
data[JSON_API_PORT] = api_port_;
data[JSON_API_USER] = api_user_;
data[JSON_MOUNT_LOCATIONS] = to_json(locations_);
if (utils::file::write_json_file(config_file, data)) {
return;
}
utils::error::raise_error(
function_name, utils::get_last_error_code(),
fmt::format("failed to save file|{}", config_file));
} catch (const std::exception &ex) {
utils::error::raise_error(
function_name, ex, fmt::format("failed to save file|{}", config_file));
}
}
void mgmt_app_config::set_api_port(std::uint16_t api_port) {
if (api_port_ == api_port) {
return;
}
api_port_ = api_port;
save();
}
void mgmt_app_config::set_mount_location(provider_type prov,
std::string_view name,
std::string_view location) {
recur_mutex_lock lock(mtx_);
if (locations_[prov][std::string{name}] == std::string{location}) {
return;
}
locations_[prov][std::string{name}] = std::string{location};
save();
}
} // namespace repertory::ui