Compare commits

...

2 Commits

Author SHA1 Message Date
7d5c252e89 test
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good
2025-03-01 11:09:11 -06:00
0aeb69a050 grab mount settings and status 2025-03-01 10:34:13 -06:00
5 changed files with 144 additions and 78 deletions

View File

@ -58,7 +58,13 @@ private:
console_consumer console; console_consumer console;
private: private:
[[nodiscard]] static auto read_process(provider_type type, void handle_get_mount(auto &&req, auto &&res) const;
void handle_get_mount_list(auto &&res) const;
void handle_get_mount_status(auto &&req, auto &&res) const;
[[nodiscard]] static auto read_process(provider_type prov,
std::string_view name, std::string_view name,
std::string_view command) std::string_view command)
-> std::vector<std::string>; -> std::vector<std::string>;

View File

@ -25,7 +25,6 @@
#include "events/event_system.hpp" #include "events/event_system.hpp"
#include "rpc/common.hpp" #include "rpc/common.hpp"
#include "types/repertory.hpp" #include "types/repertory.hpp"
#include "utils/collection.hpp"
#include "utils/error_utils.hpp" #include "utils/error_utils.hpp"
#include "utils/file.hpp" #include "utils/file.hpp"
#include "utils/path.hpp" #include "utils/path.hpp"
@ -72,11 +71,49 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
res.status = http_error_codes::internal_error; res.status = http_error_codes::internal_error;
}); });
server->Get("/api/v1/mount", [](const httplib::Request &req, auto &&res) { server->Get("/api/v1/mount",
[this](auto &&req, auto &&res) { handle_get_mount(req, res); });
server->Get("/api/v1/mount_list", [this](auto && /* req */, auto &&res) {
handle_get_mount_list(res);
});
server->Get("/api/v1/mount_status",
[this](const httplib::Request &req, auto &&res) {
handle_get_mount_status(req, res);
});
event_system::instance().start();
static std::atomic<httplib::Server *> this_server{server_};
static const auto quit_handler = [](int /* sig */) {
auto *ptr = this_server.load();
if (ptr == nullptr) {
return;
}
this_server = nullptr;
ptr->stop();
};
std::signal(SIGINT, quit_handler);
#if !defined(_WIN32)
std::signal(SIGQUIT, quit_handler);
#endif // !defined(_WIN32)
std::signal(SIGTERM, quit_handler);
server_->listen("127.0.0.1", config_->get_api_port());
this_server = nullptr;
}
handlers::~handlers() { event_system::instance().stop(); }
void handlers::handle_get_mount(auto &&req, auto &&res) const {
REPERTORY_USES_FUNCTION_NAME();
auto prov = provider_type_from_string(req.get_param_value("type")); auto prov = provider_type_from_string(req.get_param_value("type"));
auto lines = auto lines = handlers::read_process(prov, req.get_param_value("name"), "-dc");
handlers::read_process(prov, req.get_param_value("name"), "-dc");
if (lines.at(0U) != "0") { if (lines.at(0U) != "0") {
throw utils::error::create_exception(function_name, { throw utils::error::create_exception(function_name, {
@ -90,11 +127,10 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
auto result = nlohmann::json::parse(utils::string::join(lines, '\n')); auto result = nlohmann::json::parse(utils::string::join(lines, '\n'));
res.set_content(result.dump(), "application/json"); res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok; res.status = http_error_codes::ok;
}); }
server->Get("/api/v1/mount_list", [](auto && /* req */, auto &&res) { void handlers::handle_get_mount_list(auto &&res) const {
auto data_dir = auto data_dir = utils::file::directory{app_config::get_root_data_directory()};
utils::file::directory{app_config::get_root_data_directory()};
nlohmann::json result; nlohmann::json result;
@ -125,52 +161,51 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
res.set_content(result.dump(), "application/json"); res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok; res.status = http_error_codes::ok;
}); }
void handlers::handle_get_mount_status(auto &&req, auto &&res) const {
REPERTORY_USES_FUNCTION_NAME();
server->Get("/api/v1/mount_status", [](const httplib::Request &req,
auto &&res) {
auto prov = provider_type_from_string(req.get_param_value("type")); auto prov = provider_type_from_string(req.get_param_value("type"));
auto status_name = app_config::get_provider_display_name(prov);
auto name = req.get_param_value("name");
auto lines = switch (prov) {
handlers::read_process(prov, req.get_param_value("name"), "-status"); case provider_type::encrypt:
break;
auto result = nlohmann::json::parse(utils::string::join(lines, '\n')); case provider_type::remote: {
auto parts = utils::string::split(name, '_', false);
status_name = fmt::format("{}{}:{}", status_name, parts[0U], parts[1U]);
} break;
case provider_type::sia:
case provider_type::s3:
status_name = fmt::format("{}{}", status_name, name);
break;
default:
throw utils::error::create_exception(
function_name, {
fmt::format("`{}` is not supported", name),
});
}
auto lines = handlers::read_process(prov, name, "-status");
auto result =
nlohmann::json::parse(utils::string::join(lines, '\n')).at(status_name);
res.set_content(result.dump(), "application/json"); res.set_content(result.dump(), "application/json");
res.status = http_error_codes::ok; res.status = http_error_codes::ok;
});
event_system::instance().start();
static std::atomic<httplib::Server *> this_server{server_};
static const auto quit_handler = [](int /* sig */) {
auto *ptr = this_server.load();
if (ptr == nullptr) {
return;
} }
this_server = nullptr; auto handlers::read_process(provider_type prov, std::string_view name,
ptr->stop();
};
std::signal(SIGINT, quit_handler);
#if !defined(_WIN32)
std::signal(SIGQUIT, quit_handler);
#endif // !defined(_WIN32)
std::signal(SIGTERM, quit_handler);
server_->listen("127.0.0.1", config_->get_api_port());
this_server = nullptr;
}
handlers::~handlers() { event_system::instance().stop(); }
auto handlers::read_process(provider_type type, std::string_view name,
std::string_view command) std::string_view command)
-> std::vector<std::string> { -> std::vector<std::string> {
REPERTORY_USES_FUNCTION_NAME(); REPERTORY_USES_FUNCTION_NAME();
std::string str_type; std::string str_type;
switch (type) { switch (prov) {
case provider_type::encrypt: case provider_type::encrypt:
str_type = "-en"; str_type = "-en";
break; break;

View File

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
if [ "${PROJECT_BUILD_ARCH}" == "aarch64" ]; then # if [ "${PROJECT_BUILD_ARCH}" == "aarch64" ]; then
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes # docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
fi # fi
if [ "${DOCKER_NAME}" == "mingw64" ]; then if [ "${DOCKER_NAME}" == "mingw64" ]; then
APP_VERSION_BUILD_ARGS=${PROJECT_MINGW64_DOCKER_BUILD_ARGS} APP_VERSION_BUILD_ARGS=${PROJECT_MINGW64_DOCKER_BUILD_ARGS}

View File

@ -7,7 +7,7 @@ import 'package:repertory/types/mount_config.dart';
class Mount with ChangeNotifier { class Mount with ChangeNotifier {
final MountConfig mountConfig; final MountConfig mountConfig;
Mount(this.mountConfig) { Mount(this.mountConfig) {
_fetch(); refresh();
} }
String get name => mountConfig.name; String get name => mountConfig.name;
@ -20,15 +20,33 @@ class Mount with ChangeNotifier {
), ),
); );
if (response.statusCode == 200) { if (response.statusCode != 200) {
return;
}
mountConfig.updateSettings(jsonDecode(response.body)); mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners(); notifyListeners();
return;
}
} }
void refresh() { Future<void> _fetchStatus() async {
_fetch(); final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${Uri.base.origin}/api/v1/mount_status?name=$name&type=$type',
),
),
);
if (response.statusCode != 200) {
return;
}
mountConfig.updateStatus(jsonDecode(response.body));
}
Future<void> refresh() async {
await _fetch();
return _fetchStatus();
} }
} }

View File

@ -4,9 +4,12 @@ class MountConfig {
final String _name; final String _name;
final String _type; final String _type;
Map<String, dynamic> _settings = {}; Map<String, dynamic> _settings = {};
Map<String, dynamic> _status = {};
MountConfig({required name, required type}) : _name = name, _type = type; MountConfig({required name, required type}) : _name = name, _type = type;
UnmodifiableMapView get items => UnmodifiableMapView(_settings); UnmodifiableMapView get settings => UnmodifiableMapView(_settings);
UnmodifiableMapView get status => UnmodifiableMapView(_status);
String get name => _name; String get name => _name;
String get type => _type; String get type => _type;
@ -17,4 +20,8 @@ class MountConfig {
void updateSettings(Map<String, dynamic> settings) { void updateSettings(Map<String, dynamic> settings) {
_settings = settings; _settings = settings;
} }
void updateStatus(Map<String, dynamic> status) {
_status = status;
}
} }