Skip to content

Commit

Permalink
feat: 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 Mar 8, 2024
1 parent 6be0e67 commit 7a58da3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
63 changes: 59 additions & 4 deletions controller/EmbeddedNetworkController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,61 @@ 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";

static char appUiDir[16384];
sprintf(appUiDir,"%s/%s",_ztPath.c_str(),appUiPath.c_str());

auto ret = s.set_mount_point(appUiPath, appUiDir);
sv6.set_mount_point(appUiPath, appUiDir);
if (!ret) {
fprintf(stderr, "Mounting app directory failed. Creating it. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir);
if (!OSUtils::mkdir(appUiDir)) {
fprintf(stderr, "Could not create app directory either. Path: %s - Dir: %s\n", appUiPath.c_str(), appUiDir);
} else {
ret = s.set_mount_point(appUiPath, appUiDir);
sv6.set_mount_point(appUiPath, appUiDir);
if (!ret) {
fprintf(stderr, "Really could not create and mount directory. Path: %s - Dir: %s\nWeb apps won't work.\n", appUiPath.c_str(), appUiDir);
}
}
}

if (ret) {
//static file server for controller-ui
auto indexFallbackGet = [&, setContent](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");

std::string indexHtml;

if (!OSUtils::readFile(indexHtmlPath, indexHtml)) {
res.status = 500;
return;
}

res.set_content(indexHtml.c_str(), "text/html");
} else {
res.status = 500;
return;
}
};

auto slashRedirect = [&, setContent](const httplib::Request &req, httplib::Response &res) {
res.status = 301;
res.set_header("location", req.path + "/");
};

// fallback to index.html for unknown paths/files
s.Get(appUiPath + R"(/(\w+)/(.*))", indexFallbackGet);
sv6.Get(appUiPath + R"(/(\w+)/(.*))", indexFallbackGet);

// auto fix no trailing slash by redirecting
s.Get(appUiPath + R"(/(\w+))", slashRedirect);
sv6.Get(appUiPath + R"(/(\w+))", slashRedirect);
}

auto controllerGet = [&, setContent](const httplib::Request &req, httplib::Response &res) {
char tmp[4096];
Expand All @@ -887,11 +942,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
5 changes: 5 additions & 0 deletions service/OneService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,11 @@ class OneServiceImpl : public OneService
isAuth = true;
}

// Web Apps base path
if (req.path.rfind("/app", 0) == 0) { //starts with /app
isAuth = true;
}

if (!isAuth) {
// check auth token
if (req.has_header("x-zt1-auth")) {
Expand Down

0 comments on commit 7a58da3

Please sign in to comment.