Skip to content

Commit

Permalink
wip - static file server
Browse files Browse the repository at this point in the history
this lets you host web apps out of
:9993/app/{app_name}
:9993/app/{other_app}

from $ZT_HOME/app/{app_name}
  • Loading branch information
laduke committed Feb 29, 2024
1 parent 99ef1e2 commit cb0c3f3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
56 changes: 52 additions & 4 deletions controller/EmbeddedNetworkController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions service/OneService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit cb0c3f3

Please sign in to comment.