diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index a838946447..11e956e127 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -875,6 +875,54 @@ void EmbeddedNetworkController::configureHTTPControlPlane( std::string memberListPath = "/controller/network/([0-9a-fA-F]{16})/member"; std::string memberListPath2 = "/unstable/controller/network/([0-9a-fA-F]{16})/member"; std::string memberPath = "/controller/network/([0-9a-fA-F]{16})/member/([0-9a-fA-F]{10})"; + std::string appUiPath = "/app"; + + std::string homeDir(OSUtils::platformDefaultHomePath()); + static char appUiDir[16384]; + sprintf(appUiDir,"%s/%s",homeDir.c_str(),appUiPath.c_str()); + + + //static file server for controller-ui + auto ret = s.set_mount_point(appUiPath, appUiDir); + if (!ret) { + fprintf(stderr, "Mounting app directory failed. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir); + } else { + s.set_file_request_handler([](const httplib::Request &req, httplib::Response &res) { + fprintf(stderr, "post handler: %s\n", req.path.c_str()); + }); + + // fallback to index.html for unknown paths/files + s.Get(appUiPath + R"(/(\w+)/(.*))", [&](const httplib::Request& req, httplib::Response& res) { + auto match = req.matches[1]; + if (match.matched) { + char indexHtmlPath[16384]; + sprintf(indexHtmlPath,"%s/%s/%s", appUiDir, match.str().c_str(), "index.html"); + fprintf(stderr, "matched: %s\n", match.str().c_str()); + + std::string indexHtml; + + if (!OSUtils::readFile(indexHtmlPath, indexHtml)) { + fprintf(stderr, "Reading index.html failed: %s\n", indexHtmlPath); + res.status = 500; + return; + } + + res.set_content(indexHtml.c_str(), "text/html"); + } else { + fprintf(stderr, "no match: %s\n", req.path.c_str()); + res.status = 500; + return; + } + }); + + // fix no trailing slash + s.Get(appUiPath + R"(/(\w+))", [&](const httplib::Request& req, httplib::Response& res) { + fprintf(stderr, "fix: %s\n", req.path.c_str()); + res.status = 301; + res.set_header("location", req.path + "/"); + }); + + } auto controllerGet = [&, setContent](const httplib::Request &req, httplib::Response &res) { char tmp[4096]; @@ -887,11 +935,11 @@ void EmbeddedNetworkController::configureHTTPControlPlane( (unsigned long long)OSUtils::now(), dbOk ? "true" : "false"); - if (!dbOk) { - res.status = 503; - } + if (!dbOk) { + res.status = 503; + } - setContent(req, res, tmp); + setContent(req, res, tmp); }; s.Get(controllerPath, controllerGet); sv6.Get(controllerPath, controllerGet); diff --git a/service/OneService.cpp b/service/OneService.cpp index fcebe46777..4e8c4fe0d8 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -1597,6 +1597,9 @@ class OneServiceImpl : public OneService setContent(req, res, "{}"); res.status = 401; return httplib::Server::HandlerResponse::Handled; + // Web Apps base path + } else if (req.path.rfind("/app", 0) == 0) { //starts with /app + return httplib::Server::HandlerResponse::Unhandled; } else { std::string r = req.remote_addr; InetAddress remoteAddr(r.c_str());