Skip to content

Commit

Permalink
Render the explorer file manager on directory pages (#39)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 13, 2024
1 parent ba13d6d commit 841349d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/enterprise/enterprise_explorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

static auto
explorer_start(const sourcemeta::hydra::http::ServerRequest &request,
const std::string &server_base_url, const std::string &title,
const std::string &description) -> std::ostringstream {
const std::string &server_base_url, const std::string &site_name,
const std::string &title, const std::string &description)
-> std::ostringstream {
std::ostringstream html;
html << "<!DOCTYPE html>";
html << "<html lang=\"en\">";
Expand Down Expand Up @@ -49,9 +50,9 @@ explorer_start(const sourcemeta::hydra::http::ServerRequest &request,
html << "<nav class=\"navbar navbar-expand border-bottom bg-body p-3\">";
html << "<div class=\"container-fluid\">";
html << "<a class=\"navbar-brand\" href=\"" << server_base_url << "\">";
html << "<img src=\"/icon.svg\" alt=\"" << title
html << "<img src=\"/icon.svg\" alt=\"" << site_name
<< "\" height=\"30\" width=\"30\" class=\"me-2\">";
html << "<span class=\"align-middle fw-bold\">" << title << "</span>";
html << "<span class=\"align-middle fw-bold\">" << site_name << "</span>";
html << "<span class=\"align-middle fw-lighter\"> Schemas</span>";
html << "</a>";
html << "</div>";
Expand Down Expand Up @@ -141,24 +142,38 @@ static auto file_manager(std::ostringstream &html,

namespace sourcemeta::registry::enterprise {

auto explore_index(const std::string &title, const std::string &description,
auto explore_index(const std::string &site_name, const std::string &title,
const std::string &description,
const std::string &server_base_url,
const std::filesystem::path &schema_base_directory,
const sourcemeta::hydra::http::ServerRequest &request,
sourcemeta::hydra::http::ServerResponse &response) -> void {
std::ostringstream html{
explorer_start(request, server_base_url, title, description)};
explorer_start(request, server_base_url, site_name, title, description)};
file_manager(html, sourcemeta::registry::path_join(schema_base_directory,
request.path()));
explorer_end(html, response, sourcemeta::hydra::http::Status::OK);
}

auto explore_not_found(const std::string &server_base_url,
auto explore_directory(const std::string &site_name,
const std::filesystem::path &directory,
const std::string &server_base_url,
const sourcemeta::hydra::http::ServerRequest &request,
sourcemeta::hydra::http::ServerResponse &response)
-> void {
std::ostringstream html{explorer_start(request, server_base_url, site_name,
request.path(), request.path())};
file_manager(html, directory);
explorer_end(html, response, sourcemeta::hydra::http::Status::OK);
}

auto explore_not_found(const std::string &site_name,
const std::string &server_base_url,
const sourcemeta::hydra::http::ServerRequest &request,
sourcemeta::hydra::http::ServerResponse &response)
-> void {
std::ostringstream html{
explorer_start(request, server_base_url, "Not Found",
explorer_start(request, server_base_url, site_name, "Not Found",
"What you are looking for is not here")};
html << "Not Found";
explorer_end(html, response, sourcemeta::hydra::http::Status::NOT_FOUND);
Expand Down
17 changes: 15 additions & 2 deletions src/enterprise/enterprise_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ auto on_index(const sourcemeta::hydra::http::ServerLogger &,
static const auto SERVER_DESCRIPTION{
configuration().at("description").to_string()};
static const auto SCHEMAS_BASE_DIRECTORY{*(__global_data) / "schemas"};
explore_index(SERVER_TITLE, SERVER_DESCRIPTION, SERVER_BASE_URL,
explore_index(SERVER_TITLE, SERVER_TITLE, SERVER_DESCRIPTION, SERVER_BASE_URL,
SCHEMAS_BASE_DIRECTORY, request, response);
}

Expand All @@ -41,8 +41,21 @@ auto on_request(const sourcemeta::hydra::http::ServerLogger &logger,
return;
}

static const auto SERVER_TITLE{configuration().at("title").to_string()};

// Explorer
static const auto SCHEMAS_BASE_DIRECTORY{*(__global_data) / "schemas"};
const auto directory_path{
sourcemeta::registry::path_join(SCHEMAS_BASE_DIRECTORY, request.path())};
if (std::filesystem::is_directory(directory_path)) {
static const auto SERVER_BASE_URL{configuration().at("url").to_string()};
explore_directory(SERVER_TITLE, directory_path, SERVER_BASE_URL, request,
response);
return;
}

static const auto SERVER_BASE_URL{configuration().at("url").to_string()};
explore_not_found(SERVER_BASE_URL, request, response);
explore_not_found(SERVER_TITLE, SERVER_BASE_URL, request, response);
}

auto attach(sourcemeta::hydra::http::Server &server) -> void {
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/ee/directory.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GET {{base}}/example
HTTP 200
Content-Type: text/html

0 comments on commit 841349d

Please sign in to comment.