From 0107d934e5de3d00a84cf78a480d6f11706e445d Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 25 Mar 2023 11:00:45 -0400 Subject: [PATCH] fix: Resolve req paths correctly in Admin WebApp --- src/Spark.Admin.WebApp/public/index.html | 4 +- src/Spark.Admin.WebApp/src/Login/Login.fs | 2 +- src/server/sparkd/http/WebServer.cpp | 62 +++++++++++++++-------- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/Spark.Admin.WebApp/public/index.html b/src/Spark.Admin.WebApp/public/index.html index 5b0ac2727..9716ec17f 100644 --- a/src/Spark.Admin.WebApp/public/index.html +++ b/src/Spark.Admin.WebApp/public/index.html @@ -4,8 +4,8 @@ BTS Spark Admin - - + + diff --git a/src/Spark.Admin.WebApp/src/Login/Login.fs b/src/Spark.Admin.WebApp/src/Login/Login.fs index 7396c355e..464052862 100644 --- a/src/Spark.Admin.WebApp/src/Login/Login.fs +++ b/src/Spark.Admin.WebApp/src/Login/Login.fs @@ -52,7 +52,7 @@ let update msg model = // View let root model dispatch = - let sparkImg = "img/BTS-300.png" + let sparkImg = "public/img/BTS-300.png" let handler msg (ev:Browser.Types.Event) = !!ev.target?value |> msg |> dispatch section [ ClassName "hero is-fullheight is-primary" ] [ diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index cb2ebfe38..6c46ef4ca 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -47,6 +47,10 @@ std::string read_file(const std::string &path) { namespace bts { namespace http { +using Path = boost::filesystem::path; +using std::string; +using std::vector; + struct ServerState { std::mutex _m; utils::Dispatcher &_dispatcher; @@ -124,6 +128,8 @@ struct ServerState { if (iter != _session.end()) { GatewayRequest request; if (request.ParseFromString(msg->get_payload())) { + LOG(Http, Info, "WebServer::on_message. req: {}", + request.DebugString()); iter->second.handle( request, [this, hdl](const GatewayResponse &response) { websocketpp::lib::error_code ec; @@ -299,39 +305,55 @@ struct ServerState { con->set_body(body); } - boost::filesystem::path web_ui_path(const boost::filesystem::path &root) { - auto reldir = root / "web/ui"; - if (boost::filesystem::exists(reldir)) - return reldir; - reldir = root / "ui"; + Path admin_web_app_path(const Path &root) { + const auto reldir = root / "Spark.Admin.WebApp"; if (boost::filesystem::exists(reldir)) return reldir; if (root.has_parent_path()) - return web_ui_path(root.parent_path()); + return admin_web_app_path(root.parent_path()); return _www; } - boost::filesystem::path map_path_to_filesystem(const std::string &resource) { - std::string config = "/config"; - std::string files = "/files"; - std::string perf_trace = "/perf_trace"; - std::string docs = "/docs"; + Path map_path_to_filesystem(const std::string &resource) { + vector index_aliases{"/", "/index", "/index.html"}; + const string fonts{"/fonts"}; + const std::string config{"/config"}; + const std::string files{"/files"}; + const std::string perf_trace{"/perf_trace"}; + const std::string docs{"/docs"}; - if (resource.compare(0, config.size(), config) == 0) { - return _conf + resource.substr(config.size()); - } else if (resource.compare(0, files.size(), files) == 0) { - return _www + resource.substr(files.size()); - } else if (resource.compare(0, perf_trace.size(), perf_trace) == 0) { - return os::sparkd_wd() + "/data_files" + resource; - } else if (resource.compare(0, docs.size(), docs) == 0) { - return os::sparkd_wd() + "/web" + resource; +#define STARTS_WITH(y) ((resource.compare(0, y.size(), y)) == 0) + + if (std::find(index_aliases.begin(), index_aliases.end(), resource) != + index_aliases.end()) { + return Path{admin_web_app_path(os::get_install_path())} / "public" / + "index.html"; } - return web_ui_path(os::get_install_path()) / resource; + if (STARTS_WITH(fonts)) { + return Path{admin_web_app_path(os::get_install_path())} / "public" / + resource; + } + if (STARTS_WITH(config)) { + return _conf + resource.substr(config.size()); + } + if (STARTS_WITH(files)) { + return _www + resource.substr(files.size()); + } + if (STARTS_WITH(perf_trace)) { + return Path{os::sparkd_wd()} / "data_files" / resource; + } + if (STARTS_WITH(docs)) { + return Path{os::sparkd_wd()} / "Spark.Admin.WebApp" / resource; + } + return admin_web_app_path(os::get_install_path()) / resource; + +#undef STARTS_WITH } void return_file(connection_ptr &con, const boost::filesystem::path &path) { con->set_body(read_file(path.string())); con->set_status(websocketpp::http::status_code::ok); + LOG(Http, Info, "WebServer::return_file. path: {}", path); } void return_bts_installer(connection_ptr &con) {