From c95242e016f1c6b954c101d890291be118ed2bdf Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Thu, 27 Feb 2025 11:13:06 -0600 Subject: [PATCH] fixes and setup --- cmake/libraries/sqlite.cmake | 2 +- repertory/repertory/include/cli/actions.hpp | 2 +- repertory/repertory/include/cli/ui.hpp | 19 +++-- repertory/repertory/include/ui/handlers.hpp | 32 +++++--- repertory/repertory/src/ui/handlers.cpp | 38 +++++++++- scripts/make_common.sh | 6 +- web/repertory/lib/constants.dart | 1 + web/repertory/lib/main.dart | 83 ++------------------- 8 files changed, 80 insertions(+), 103 deletions(-) create mode 100644 web/repertory/lib/constants.dart diff --git a/cmake/libraries/sqlite.cmake b/cmake/libraries/sqlite.cmake index 49b87217..b37e8958 100644 --- a/cmake/libraries/sqlite.cmake +++ b/cmake/libraries/sqlite.cmake @@ -2,7 +2,7 @@ if(PROJECT_ENABLE_SQLITE) if(PROJECT_BUILD) add_definitions(-DPROJECT_ENABLE_SQLITE) if (PROJECT_IS_MINGW AND NOT PROJECT_IS_MINGW_UNIX) - pkg_check_modules(SQLITE3 REQUIRED sqlite3>=${SQLITE2_VERSION}) + pkg_check_modules(SQLITE3 REQUIRED sqlite3) include_directories(SYSTEM BEFORE ${SQLITE3_INCLUDE_DIRS}) link_libraries(${SQLITE3_LIBRARIES}) else() diff --git a/repertory/repertory/include/cli/actions.hpp b/repertory/repertory/include/cli/actions.hpp index af526636..95008103 100644 --- a/repertory/repertory/include/cli/actions.hpp +++ b/repertory/repertory/include/cli/actions.hpp @@ -71,7 +71,7 @@ static const std::unordered_map 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}, }; diff --git a/repertory/repertory/include/cli/ui.hpp b/repertory/repertory/include/cli/ui.hpp index ca56e1c9..f43fc132 100644 --- a/repertory/repertory/include/cli/ui.hpp +++ b/repertory/repertory/include/cli/ui.hpp @@ -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 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 diff --git a/repertory/repertory/include/ui/handlers.hpp b/repertory/repertory/include/ui/handlers.hpp index ad327990..30b6e629 100644 --- a/repertory/repertory/include/ui/handlers.hpp +++ b/repertory/repertory/include/ui/handlers.hpp @@ -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_ diff --git a/repertory/repertory/src/ui/handlers.cpp b/repertory/repertory/src/ui/handlers.cpp index 7056cad4..815badac 100644 --- a/repertory/repertory/src/ui/handlers.cpp +++ b/repertory/repertory/src/ui/handlers.cpp @@ -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 diff --git a/scripts/make_common.sh b/scripts/make_common.sh index 4436b156..dee39cb7 100755 --- a/scripts/make_common.sh +++ b/scripts/make_common.sh @@ -89,7 +89,7 @@ if [ -f "${PROJECT_SOURCE_DIR}/web/${PROJECT_NAME}/pubspec.yaml" ]; then docker build \ -t ${FLUTTER_TAG_NAME} \ - ${PROJECT_SOURCE_DIR}/docker/x86_64/flutter || exit 1 + "${PROJECT_SOURCE_DIR}/docker/x86_64/flutter" || exit 1 docker run -td \ --name ${FLUTTER_CONTAINER_NAME} \ @@ -106,8 +106,8 @@ if [ -f "${PROJECT_SOURCE_DIR}/web/${PROJECT_NAME}/pubspec.yaml" ]; then flutter build web || exit 1 fi - rsync -av --progress ${PROJECT_SOURCE_DIR}/web/${PROJECT_NAME}/build/web/ \ - ${PROJECT_DIST_DIR}/web/ + rsync -av --progress "${PROJECT_SOURCE_DIR}/web/${PROJECT_NAME}/build/web/" \ + "${PROJECT_DIST_DIR}/web/" fi if [ "${PROJECT_IS_MINGW}" == "1" ]; then diff --git a/web/repertory/lib/constants.dart b/web/repertory/lib/constants.dart new file mode 100644 index 00000000..1fb966ed --- /dev/null +++ b/web/repertory/lib/constants.dart @@ -0,0 +1 @@ +const String APP_TITLE = "Repertory Management Portal"; diff --git a/web/repertory/lib/main.dart b/web/repertory/lib/main.dart index 7b7f5b6f..a535e4ab 100644 --- a/web/repertory/lib/main.dart +++ b/web/repertory/lib/main.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'constants.dart' as constants; + void main() { runApp(const MyApp()); } @@ -7,28 +9,12 @@ void main() { class MyApp extends StatelessWidget { const MyApp({super.key}); - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: constants.APP_TITLE, theme: ThemeData( - // This is the theme of your application. - // - // TRY THIS: Try running your application with "flutter run". You'll see - // the application has a purple toolbar. Then, without quitting the app, - // try changing the seedColor in the colorScheme below to Colors.green - // and then invoke "hot reload" (save your changes or press the "hot - // reload" button in a Flutter-supported IDE, or press "r" if you used - // the command line to start the app). - // - // Notice that the counter didn't reset back to zero; the application - // state is not lost during the reload. To reset the state, use hot - // restart instead. - // - // This works for code too, not just values: Most code changes can be - // tested with just a hot reload. - colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange), ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); @@ -38,15 +24,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - final String title; @override @@ -54,69 +31,19 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - @override Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. return Scaffold( appBar: AppBar( - // TRY THIS: Try changing the color here to a specific color (to - // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar - // change color while the other colors stay the same. backgroundColor: Theme.of(context).colorScheme.inversePrimary, - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - // - // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" - // action in the IDE, or press "p" in the console), to see the - // wireframe for each widget. mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text('You have pushed the button this many times:'), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - ], + children: [const Text('TODO-Everything')], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. ); } }