diff --git a/repertory/librepertory/include/types/repertory.hpp b/repertory/librepertory/include/types/repertory.hpp index 38be342e..b5a3fed5 100644 --- a/repertory/librepertory/include/types/repertory.hpp +++ b/repertory/librepertory/include/types/repertory.hpp @@ -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"}; diff --git a/repertory/repertory/include/ui/mgmt_app_config.hpp b/repertory/repertory/include/ui/mgmt_app_config.hpp index 92978254..f0a93908 100644 --- a/repertory/repertory/include/ui/mgmt_app_config.hpp +++ b/repertory/repertory/include/ui/mgmt_app_config.hpp @@ -26,6 +26,9 @@ namespace repertory::ui { class mgmt_app_config final { +public: + mgmt_app_config(); + private: atomic api_auth_{"test"}; std::atomic 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_; } diff --git a/repertory/repertory/src/ui/mgmt_app_config.cpp b/repertory/repertory/src/ui/mgmt_app_config.cpp index e880c786..2cd613ba 100644 --- a/repertory/repertory/src/ui/mgmt_app_config.cpp +++ b/repertory/repertory/src/ui/mgmt_app_config.cpp @@ -21,7 +21,39 @@ */ #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 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(); + api_port_ = data.at(JSON_API_PORT).get(); + api_user_ = data.at(JSON_API_USER).get(); + locations_ = data.at(JSON_MOUNT_LOCATIONS).get(); + 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,8 +66,42 @@ 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] = 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) { api_port_ = api_port; + save(); } void mgmt_app_config::set_mount_location(provider_type prov,