Skip to content

Commit

Permalink
Consolidate server functionality all in src/server
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jan 7, 2025
1 parent dccb68b commit 287978d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 59 deletions.
45 changes: 0 additions & 45 deletions src/enterprise/enterprise_server.h

This file was deleted.

8 changes: 2 additions & 6 deletions src/enterprise/server.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
target_compile_definitions(schema_registry_server PRIVATE SOURCEMETA_REGISTRY_ENTERPRISE)

cmake_path(GET CMAKE_CURRENT_LIST_FILE PARENT_PATH ENTERPRISE_SOURCE_DIR)
target_sources(schema_registry_server PRIVATE
"${ENTERPRISE_SOURCE_DIR}/enterprise_server.h"
"${CMAKE_CURRENT_BINARY_DIR}/style.min.css")
target_include_directories(schema_registry_server PRIVATE "${ENTERPRISE_SOURCE_DIR}")
target_sources(schema_registry_server PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/style.min.css")

# Static assets
cmake_path(GET CMAKE_CURRENT_LIST_FILE PARENT_PATH ENTERPRISE_SOURCE_DIR)
target_compile_definitions(schema_registry_server
PRIVATE SOURCEMETA_REGISTRY_ENTERPRISE_STATIC="${REGISTRY_PREFIX}/share/sourcemeta/registry")
include(BootstrapFiles)
Expand Down
35 changes: 27 additions & 8 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,33 @@ static auto on_request(const sourcemeta::hydra::http::ServerLogger &logger,
const sourcemeta::hydra::http::ServerRequest &request,
sourcemeta::hydra::http::ServerResponse &response)
-> void {
const auto &request_path{request.path()};

#ifdef SOURCEMETA_REGISTRY_ENTERPRISE
if (!request_path.ends_with(".json")) {
const auto asset_path{SOURCEMETA_REGISTRY_ENTERPRISE_STATIC + request_path};
if (std::filesystem::exists(asset_path)) {
sourcemeta::hydra::http::serve_file(asset_path, request, response);
return;
}

const auto directory_path{sourcemeta::registry::path_join(
*(__global_data) / "generated", request.path())};
if (std::filesystem::is_directory(directory_path)) {
sourcemeta::hydra::http::serve_file(directory_path / "index.html",
request, response);
} else {
sourcemeta::hydra::http::serve_file(
*(__global_data) / "generated" / "404.html", request, response,
sourcemeta::hydra::http::Status::NOT_FOUND);
}

return;
}
#endif

const auto schema_identifier{sourcemeta::registry::request_path_to_schema_uri(
configuration().at("url").to_string(), request.path())};
configuration().at("url").to_string(), request_path)};
const auto maybe_schema{resolver(schema_identifier)};
if (!maybe_schema.has_value()) {
json_error(logger, request, response,
Expand Down Expand Up @@ -153,10 +178,6 @@ static auto on_error(std::exception_ptr exception_ptr,
}
}

#ifdef SOURCEMETA_REGISTRY_ENTERPRISE
#include "enterprise_server.h"
#endif

// We try to keep this function as straight to point as possible
// with minimal input validation (outside debug builds). The intention
// is for the server to start running and bind to the port as quickly
Expand All @@ -182,12 +203,10 @@ auto main(int argc, char *argv[]) noexcept -> int {

sourcemeta::hydra::http::Server server;
#ifdef SOURCEMETA_REGISTRY_ENTERPRISE
sourcemeta::registry::enterprise::attach(server);
server.route(sourcemeta::hydra::http::Method::GET, "/", on_index);
#else
#endif
server.route(sourcemeta::hydra::http::Method::GET, "/*", on_request);
server.route(sourcemeta::hydra::http::Method::HEAD, "/*", on_request);
#endif
server.otherwise(on_otherwise);
server.error(on_error);

Expand Down

0 comments on commit 287978d

Please sign in to comment.