Compare commits
7 Commits
06ad2a62cb
...
ce1a3c416c
Author | SHA1 | Date | |
---|---|---|---|
ce1a3c416c | |||
2ff16230d1 | |||
2096669a7f | |||
4a59917d23 | |||
083406a85d | |||
b513ef7545 | |||
cc301e207c |
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include "types/repertory.hpp"
|
#include "types/repertory.hpp"
|
||||||
#include "ui/handlers.hpp"
|
#include "ui/handlers.hpp"
|
||||||
|
#include "ui/mgmt_app_config.hpp"
|
||||||
#include "utils/cli_utils.hpp"
|
#include "utils/cli_utils.hpp"
|
||||||
#include "utils/file.hpp"
|
#include "utils/file.hpp"
|
||||||
#include "utils/string.hpp"
|
#include "utils/string.hpp"
|
||||||
@ -51,11 +52,7 @@ ui(std::vector<const char *> args, const std::string & /*data_directory*/,
|
|||||||
return exit_code::ui_mount_failed;
|
return exit_code::ui_mount_failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
ui::mgmt_app_config config{
|
ui::mgmt_app_config config{};
|
||||||
.api_auth_ = "test",
|
|
||||||
.api_port_ = ui_port,
|
|
||||||
.api_user_ = "test",
|
|
||||||
};
|
|
||||||
ui::handlers handlers(&config, &server);
|
ui::handlers handlers(&config, &server);
|
||||||
|
|
||||||
return exit_code::success;
|
return exit_code::success;
|
||||||
|
@ -25,17 +25,7 @@
|
|||||||
#include "events/consumers/console_consumer.hpp"
|
#include "events/consumers/console_consumer.hpp"
|
||||||
|
|
||||||
namespace repertory::ui {
|
namespace repertory::ui {
|
||||||
struct mgmt_app_config final {
|
class mgmt_app_config;
|
||||||
std::string api_auth_{"test"};
|
|
||||||
std::uint16_t api_port_{default_ui_mgmt_port};
|
|
||||||
std::string api_user_{"test"};
|
|
||||||
|
|
||||||
[[nodiscard]] auto get_api_auth() const -> std::string { return api_auth_; }
|
|
||||||
|
|
||||||
[[nodiscard]] auto get_api_port() const -> std::uint16_t { return api_port_; }
|
|
||||||
|
|
||||||
[[nodiscard]] auto get_api_user() const -> std::string { return api_user_; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class handlers final {
|
class handlers final {
|
||||||
public:
|
public:
|
||||||
@ -52,6 +42,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
mgmt_app_config *config_;
|
mgmt_app_config *config_;
|
||||||
|
std::string repertory_binary_;
|
||||||
httplib::Server *server_;
|
httplib::Server *server_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -62,15 +53,16 @@ private:
|
|||||||
|
|
||||||
void handle_get_mount_list(auto &&res) const;
|
void handle_get_mount_list(auto &&res) const;
|
||||||
|
|
||||||
|
void handle_get_mount_location(auto &&req, auto &&res) const;
|
||||||
|
|
||||||
void handle_get_mount_status(auto &&req, auto &&res) const;
|
void handle_get_mount_status(auto &&req, auto &&res) const;
|
||||||
|
|
||||||
void handle_post_mount(auto &&req, auto &&res) const;
|
void handle_post_mount(auto &&req, auto &&res) const;
|
||||||
|
|
||||||
void handle_put_set_value_by_name(auto &&req, auto &&res);
|
void handle_put_set_value_by_name(auto &&req, auto &&res);
|
||||||
|
|
||||||
static auto read_process(provider_type prov, std::string_view name,
|
auto read_process(provider_type prov, std::string_view name,
|
||||||
std::string_view command)
|
std::string_view command) const -> std::vector<std::string>;
|
||||||
-> std::vector<std::string>;
|
|
||||||
};
|
};
|
||||||
} // namespace repertory::ui
|
} // namespace repertory::ui
|
||||||
|
|
||||||
|
54
repertory/repertory/include/ui/mgmt_app_config.hpp
Normal file
54
repertory/repertory/include/ui/mgmt_app_config.hpp
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
Copyright <2018-2025> <scott.e.graves@protonmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_
|
||||||
|
#define REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_
|
||||||
|
|
||||||
|
#include "types/repertory.hpp"
|
||||||
|
|
||||||
|
namespace repertory::ui {
|
||||||
|
class mgmt_app_config final {
|
||||||
|
private:
|
||||||
|
std::string api_auth_{"test"};
|
||||||
|
std::uint16_t api_port_{default_ui_mgmt_port};
|
||||||
|
std::string api_user_{"test"};
|
||||||
|
std::unordered_map<provider_type,
|
||||||
|
std::unordered_map<std::string, std::string>>
|
||||||
|
locations_;
|
||||||
|
mutable std::recursive_mutex mtx_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
[[nodiscard]] auto get_api_auth() const -> std::string { return api_auth_; }
|
||||||
|
|
||||||
|
[[nodiscard]] auto get_api_port() const -> std::uint16_t { return api_port_; }
|
||||||
|
|
||||||
|
[[nodiscard]] auto get_api_user() const -> std::string { return api_user_; }
|
||||||
|
|
||||||
|
[[nodiscard]] auto get_mount_location(provider_type prov,
|
||||||
|
std::string_view name) const
|
||||||
|
-> std::string;
|
||||||
|
|
||||||
|
void set_mount_location(provider_type prov, std::string_view name,
|
||||||
|
std::string_view location);
|
||||||
|
};
|
||||||
|
} // namespace repertory::ui
|
||||||
|
|
||||||
|
#endif // REPERTORY_INCLUDE_UI_MGMT_APP_CONFIG_HPP_
|
@ -25,6 +25,7 @@
|
|||||||
#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 "ui/mgmt_app_config.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"
|
||||||
@ -32,7 +33,13 @@
|
|||||||
|
|
||||||
namespace repertory::ui {
|
namespace repertory::ui {
|
||||||
handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
||||||
: config_(config), server_(server) {
|
: config_(config),
|
||||||
|
#if defined(_WIN32)
|
||||||
|
repertory_binary_(utils::path::combine(".", {"repertory.exe"})),
|
||||||
|
#else // !defined(_WIN32)
|
||||||
|
repertory_binary_(utils::path::combine(".", {"repertory"})),
|
||||||
|
#endif // defined(_WIN32)
|
||||||
|
server_(server) {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
server_->set_pre_routing_handler(
|
server_->set_pre_routing_handler(
|
||||||
@ -74,6 +81,10 @@ handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
|||||||
server->Get("/api/v1/mount",
|
server->Get("/api/v1/mount",
|
||||||
[this](auto &&req, auto &&res) { handle_get_mount(req, res); });
|
[this](auto &&req, auto &&res) { handle_get_mount(req, res); });
|
||||||
|
|
||||||
|
server->Get("/api/v1/mount_location", [this](auto &&req, auto &&res) {
|
||||||
|
handle_get_mount_location(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
server->Get("/api/v1/mount_list", [this](auto && /* req */, auto &&res) {
|
server->Get("/api/v1/mount_list", [this](auto && /* req */, auto &&res) {
|
||||||
handle_get_mount_list(res);
|
handle_get_mount_list(res);
|
||||||
});
|
});
|
||||||
@ -133,7 +144,7 @@ void handlers::handle_get_mount(auto &&req, auto &&res) const {
|
|||||||
|
|
||||||
auto prov = provider_type_from_string(req.get_param_value("type"));
|
auto prov = provider_type_from_string(req.get_param_value("type"));
|
||||||
|
|
||||||
auto lines = handlers::read_process(prov, req.get_param_value("name"), "-dc");
|
auto lines = launch_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, {
|
||||||
@ -183,13 +194,26 @@ void handlers::handle_get_mount_list(auto &&res) const {
|
|||||||
res.status = http_error_codes::ok;
|
res.status = http_error_codes::ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handlers::handle_get_mount_location(auto &&req, auto &&res) const {
|
||||||
|
auto name = req.get_param_value("name");
|
||||||
|
auto prov = provider_type_from_string(req.get_param_value("type"));
|
||||||
|
|
||||||
|
res.set_content(
|
||||||
|
nlohmann::json({
|
||||||
|
{"Location", config_->get_mount_location(prov, name)},
|
||||||
|
})
|
||||||
|
.dump(),
|
||||||
|
"application/json");
|
||||||
|
res.status = http_error_codes::ok;
|
||||||
|
}
|
||||||
|
|
||||||
void handlers::handle_get_mount_status(auto &&req, auto &&res) const {
|
void handlers::handle_get_mount_status(auto &&req, auto &&res) const {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
auto type = req.get_param_value("type");
|
|
||||||
auto prov = provider_type_from_string(type);
|
|
||||||
auto status_name = app_config::get_provider_display_name(prov);
|
|
||||||
auto name = req.get_param_value("name");
|
auto name = req.get_param_value("name");
|
||||||
|
auto prov = provider_type_from_string(req.get_param_value("type"));
|
||||||
|
|
||||||
|
auto status_name = app_config::get_provider_display_name(prov);
|
||||||
|
|
||||||
switch (prov) {
|
switch (prov) {
|
||||||
case provider_type::encrypt:
|
case provider_type::encrypt:
|
||||||
@ -206,55 +230,61 @@ void handlers::handle_get_mount_status(auto &&req, auto &&res) const {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw utils::error::create_exception(
|
throw utils::error::create_exception(function_name,
|
||||||
function_name, {
|
{
|
||||||
fmt::format("`{}` is not supported", name),
|
"provider is not supported",
|
||||||
});
|
provider_type_to_string(prov),
|
||||||
|
name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto lines = handlers::read_process(prov, name, "-status");
|
auto lines = launch_process(prov, name, "-status");
|
||||||
|
|
||||||
|
nlohmann::json result(
|
||||||
|
nlohmann::json::parse(utils::string::join(lines, '\n')).at(status_name));
|
||||||
|
if (result.at("Location").get<std::string>().empty()) {
|
||||||
|
result.at("Location") = config_->get_mount_location(prov, name);
|
||||||
|
} else if (result.at("Active").get<bool>()) {
|
||||||
|
config_->set_mount_location(prov, name,
|
||||||
|
result.at("Location").get<std::string>());
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handlers::handle_post_mount(auto &&req, auto &&res) const {
|
void handlers::handle_post_mount(auto &&req, auto &&res) const {
|
||||||
auto type = req.get_param_value("type");
|
|
||||||
auto name = req.get_param_value("name");
|
|
||||||
auto location = utils::path::absolute(req.get_param_value("location"));
|
auto location = utils::path::absolute(req.get_param_value("location"));
|
||||||
|
auto name = req.get_param_value("name");
|
||||||
|
auto prov = provider_type_from_string(req.get_param_value("type"));
|
||||||
auto unmount = utils::string::to_bool(req.get_param_value("unmount"));
|
auto unmount = utils::string::to_bool(req.get_param_value("unmount"));
|
||||||
auto prov = provider_type_from_string(type);
|
|
||||||
|
|
||||||
if (unmount) {
|
if (unmount) {
|
||||||
read_process(prov, name, "-unmount");
|
launch_process(prov, name, "-unmount");
|
||||||
} else {
|
} else {
|
||||||
read_process(prov, name, fmt::format("\"{}\"", location));
|
launch_process(prov, name, fmt::format(R"("{}")", location), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status = http_error_codes::ok;
|
res.status = http_error_codes::ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handlers::handle_put_set_value_by_name(auto &&req, auto &&res) {
|
void handlers::handle_put_set_value_by_name(auto &&req, auto &&res) {
|
||||||
auto type = req.get_param_value("type");
|
|
||||||
auto name = req.get_param_value("name");
|
|
||||||
auto prov = provider_type_from_string(type);
|
|
||||||
|
|
||||||
auto key = req.get_param_value("key");
|
auto key = req.get_param_value("key");
|
||||||
|
auto name = req.get_param_value("name");
|
||||||
|
auto prov = provider_type_from_string(req.get_param_value("type"));
|
||||||
auto value = req.get_param_value("value");
|
auto value = req.get_param_value("value");
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
read_process(prov, name, fmt::format("-set {} \"{}\"", key, value));
|
launch_process(prov, name, fmt::format(R"(-set {} "{}")", key, value));
|
||||||
#else //! defined(_WIN32)
|
#else //! defined(_WIN32)
|
||||||
read_process(prov, name, fmt::format("-set {} '{}'", key, value));
|
launch_process(prov, name, fmt::format("-set {} '{}'", key, value));
|
||||||
#endif // defined(_WIN32)
|
#endif // defined(_WIN32)
|
||||||
|
|
||||||
res.status = http_error_codes::ok;
|
res.status = http_error_codes::ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto handlers::read_process(provider_type prov, std::string_view name,
|
auto handlers::launch_process(provider_type prov, std::string_view name,
|
||||||
std::string_view command)
|
std::string_view command, bool background) const
|
||||||
-> std::vector<std::string> {
|
-> std::vector<std::string> {
|
||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
@ -278,17 +308,29 @@ auto handlers::read_process(provider_type prov, std::string_view name,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw utils::error::create_exception(
|
throw utils::error::create_exception(function_name,
|
||||||
function_name, {
|
{
|
||||||
fmt::format("`{}` is not supported", name),
|
"provider is not supported",
|
||||||
});
|
provider_type_to_string(prov),
|
||||||
|
name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto cmd_line = fmt::format("repertory {} {}", str_type, command);
|
auto cmd_line =
|
||||||
|
fmt::format(R"({} {} {})", repertory_binary_, str_type, command);
|
||||||
|
|
||||||
|
if (background) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
auto *pipe = popen(cmd_line.c_str(), "r");
|
auto *pipe = popen(cmd_line.c_str(), "r");
|
||||||
if (pipe == nullptr) {
|
if (pipe == nullptr) {
|
||||||
return {};
|
throw utils::error::create_exception(function_name,
|
||||||
|
{
|
||||||
|
"failed to execute command",
|
||||||
|
provider_type_to_string(prov),
|
||||||
|
name,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string data;
|
std::string data;
|
||||||
|
43
repertory/repertory/src/ui/mgmt_app_config.cpp
Normal file
43
repertory/repertory/src/ui/mgmt_app_config.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
Copyright <2018-2025> <scott.e.graves@protonmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include "ui/mgmt_app_config.hpp"
|
||||||
|
|
||||||
|
namespace repertory::ui {
|
||||||
|
auto mgmt_app_config::get_mount_location(provider_type prov,
|
||||||
|
std::string_view name) const
|
||||||
|
-> std::string {
|
||||||
|
recur_mutex_lock lock(mtx_);
|
||||||
|
if (locations_.contains(prov) &&
|
||||||
|
locations_.at(prov).contains(std::string{name})) {
|
||||||
|
return locations_.at(prov).at(std::string{name});
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
void mgmt_app_config::set_mount_location(provider_type prov,
|
||||||
|
std::string_view name,
|
||||||
|
std::string_view location) {
|
||||||
|
recur_mutex_lock lock(mtx_);
|
||||||
|
locations_[prov][std::string{name}] = std::string{location};
|
||||||
|
}
|
||||||
|
} // namespace repertory::ui
|
@ -34,26 +34,23 @@ class _MountWidgetState extends State<MountWidget> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
isThreeLine: isActive,
|
isThreeLine: true,
|
||||||
leading: IconButton(
|
leading: IconButton(
|
||||||
icon: Icon(Icons.settings, color: textColor),
|
icon: Icon(Icons.settings, color: textColor),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.pushNamed(context, '/settings', arguments: mount);
|
Navigator.pushNamed(context, '/settings', arguments: mount);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
subtitle:
|
subtitle: Column(
|
||||||
isActive
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
? Column(
|
children: [
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
nameText,
|
||||||
children: [
|
SelectableText(
|
||||||
nameText,
|
mount.path,
|
||||||
SelectableText(
|
style: TextStyle(color: subTextColor),
|
||||||
mount.path,
|
),
|
||||||
style: TextStyle(color: subTextColor),
|
],
|
||||||
),
|
),
|
||||||
],
|
|
||||||
)
|
|
||||||
: nameText,
|
|
||||||
title: SelectableText(
|
title: SelectableText(
|
||||||
initialCaps(mount.type),
|
initialCaps(mount.type),
|
||||||
style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
|
style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
|
||||||
@ -71,8 +68,8 @@ class _MountWidgetState extends State<MountWidget> {
|
|||||||
_enabled = false;
|
_enabled = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
String? location;
|
String? location = mount.path;
|
||||||
if (!isActive) {
|
if (!isActive && mount.path.isEmpty) {
|
||||||
location = await mount.getMountLocation();
|
location = await mount.getMountLocation();
|
||||||
if (location == null) {}
|
if (location == null) {}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user