fixes and setup
This commit is contained in:
@@ -71,7 +71,7 @@ static const std::unordered_map<utils::cli::option, action, option_hasher>
|
||||
cli::actions::pinned_status},
|
||||
{utils::cli::options::set_option, cli::actions::set},
|
||||
{utils::cli::options::status_option, cli::actions::status},
|
||||
{utils::cli::options::ui, cli::actions::ui},
|
||||
{utils::cli::options::ui_option, cli::actions::ui},
|
||||
{utils::cli::options::unmount_option, cli::actions::unmount},
|
||||
{utils::cli::options::unpin_file_option, cli::actions::unpin_file},
|
||||
};
|
||||
|
@@ -25,6 +25,7 @@
|
||||
#include "types/repertory.hpp"
|
||||
#include "ui/handlers.hpp"
|
||||
#include "utils/cli_utils.hpp"
|
||||
#include "utils/file.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::cli::actions {
|
||||
@@ -41,18 +42,22 @@ ui(std::vector<const char *> args, const std::string & /*data_directory*/,
|
||||
ui_port = utils::string::to_uint16(data);
|
||||
}
|
||||
|
||||
utils::file::change_to_process_directory();
|
||||
|
||||
httplib::Server srv;
|
||||
if (not srv.set_mount_point("/", "./web")) {
|
||||
if (not utils::file::change_to_process_directory()) {
|
||||
return exit_code::ui_mount_failed;
|
||||
}
|
||||
|
||||
{
|
||||
ui::handlers(&srv);
|
||||
srv.listen("127.0.0.1", ui_port);
|
||||
httplib::Server server;
|
||||
if (not server.set_mount_point("/ui", "./web")) {
|
||||
return exit_code::ui_mount_failed;
|
||||
}
|
||||
|
||||
ui::mgmt_app_config config{
|
||||
.api_auth_ = "test",
|
||||
.api_port_ = ui_port,
|
||||
.api_user_ = "test",
|
||||
};
|
||||
ui::handlers handlers(&config, &server);
|
||||
|
||||
return exit_code::success;
|
||||
}
|
||||
} // namespace repertory::cli::actions
|
||||
|
@@ -22,32 +22,44 @@
|
||||
#ifndef REPERTORY_INCLUDE_UI_HANDLERS_HPP_
|
||||
#define REPERTORY_INCLUDE_UI_HANDLERS_HPP_
|
||||
|
||||
#include "types/repertory.hpp"
|
||||
#include "events/consumers/console_consumer.hpp"
|
||||
|
||||
namespace repertory::ui {
|
||||
struct mgmt_app_config final {
|
||||
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_; }
|
||||
};
|
||||
|
||||
namespace repertory {
|
||||
class app_config;
|
||||
namespace ui {
|
||||
class handlers final {
|
||||
public:
|
||||
handlers(app_config *config, httplib::Server *server);
|
||||
handlers(mgmt_app_config *config, httplib::Server *server);
|
||||
|
||||
handlers() = delete;
|
||||
handlers(const handlers &) = delete;
|
||||
handlers(handlers &&) = delete;
|
||||
|
||||
~handlers() = default;
|
||||
~handlers();
|
||||
|
||||
auto operator=(const handlers &) -> handlers & = delete;
|
||||
auto operator=(handlers &&) -> handlers & = delete;
|
||||
|
||||
private:
|
||||
app_config *config_{nullptr};
|
||||
httplib::Server *server_{nullptr};
|
||||
mgmt_app_config *config_;
|
||||
httplib::Server *server_;
|
||||
|
||||
private:
|
||||
console_consumer console{};
|
||||
|
||||
private:
|
||||
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
|
||||
};
|
||||
} // namespace ui
|
||||
} // namespace repertory
|
||||
} // namespace repertory::ui
|
||||
|
||||
#endif // REPERTORY_INCLUDE_UI_HANDLERS_HPP_
|
||||
|
@@ -21,12 +21,17 @@
|
||||
*/
|
||||
#include "ui/handlers.hpp"
|
||||
|
||||
#include "app_config.hpp"
|
||||
#include "events/event_system.hpp"
|
||||
#include "types/repertory.hpp"
|
||||
#include "utils/base64.hpp"
|
||||
#include "utils/error_utils.hpp"
|
||||
#include "utils/string.hpp"
|
||||
|
||||
namespace repertory::ui {
|
||||
handlers::handlers(app_config *config, httplib::Server *server)
|
||||
handlers::handlers(mgmt_app_config *config, httplib::Server *server)
|
||||
: config_(config), server_(server) {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
server_->set_pre_routing_handler(
|
||||
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
|
||||
if (check_authorization(req)) {
|
||||
@@ -36,8 +41,36 @@ handlers::handlers(app_config *config, httplib::Server *server)
|
||||
res.status = http_error_codes::unauthorized;
|
||||
return httplib::Server::HandlerResponse::Handled;
|
||||
});
|
||||
|
||||
server_->set_exception_handler([](const httplib::Request &req,
|
||||
httplib::Response &res,
|
||||
std::exception_ptr ptr) {
|
||||
json data{
|
||||
{"path", req.path},
|
||||
};
|
||||
|
||||
try {
|
||||
std::rethrow_exception(ptr);
|
||||
} catch (const std::exception &e) {
|
||||
data["error"] = (e.what() == nullptr) ? "unknown error" : e.what();
|
||||
utils::error::raise_error(function_name, e,
|
||||
"failed request: " + req.path);
|
||||
} catch (...) {
|
||||
data["error"] = "unknown error";
|
||||
utils::error::raise_error(function_name, "unknown error",
|
||||
"failed request: " + req.path);
|
||||
}
|
||||
|
||||
res.set_content(data.dump(), "application/json");
|
||||
res.status = http_error_codes::internal_error;
|
||||
});
|
||||
|
||||
event_system::instance().start();
|
||||
server_->listen("127.0.0.1", config_->get_api_port());
|
||||
}
|
||||
|
||||
handlers::~handlers() { event_system::instance().stop(); }
|
||||
|
||||
auto handlers::check_authorization(const httplib::Request &req) -> bool {
|
||||
REPERTORY_USES_FUNCTION_NAME();
|
||||
|
||||
@@ -85,5 +118,4 @@ auto handlers::check_authorization(const httplib::Request &req) -> bool {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace repertory::ui
|
||||
|
Reference in New Issue
Block a user