read/write ui.json
This commit is contained in:
parent
43e8db5bb6
commit
ada11d6e62
@ -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"};
|
||||
|
@ -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_; }
|
||||
|
||||
|
@ -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<std::string>();
|
||||
api_port_ = data.at(JSON_API_PORT).get<std::uint16_t>();
|
||||
api_user_ = data.at(JSON_API_USER).get<std::string>();
|
||||
locations_ = data.at(JSON_MOUNT_LOCATIONS).get<decltype(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,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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user