diff --git a/src/index/CMakeLists.txt b/src/index/CMakeLists.txt index a28969f0..199e8144 100644 --- a/src/index/CMakeLists.txt +++ b/src/index/CMakeLists.txt @@ -20,6 +20,7 @@ target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::core::alterschema target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::core::editorschema) target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::core::parallel) target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::core::options) +target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::core::uritemplate) target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::blaze::compiler) target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::blaze::evaluator) target_link_libraries(sourcemeta_one_index PRIVATE sourcemeta::blaze::output) diff --git a/src/index/generators.h b/src/index/generators.h index 20d432fb..f5df7a64 100644 --- a/src/index/generators.h +++ b/src/index/generators.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -408,6 +409,18 @@ struct GENERATE_STATS { } }; +struct GENERATE_URITEMPLATE_ROUTES { + using Context = sourcemeta::core::URITemplateRouter; + static auto + handler(const std::filesystem::path &destination, + const sourcemeta::core::BuildDependencies &, + const sourcemeta::core::BuildDynamicCallback &, + const Context &router) -> void { + std::filesystem::create_directories(destination.parent_path()); + sourcemeta::core::URITemplateRouterView::save(router, destination); + } +}; + } // namespace sourcemeta::one #endif diff --git a/src/index/index.cc b/src/index/index.cc index e5b3cf5e..6b87f9fb 100644 --- a/src/index/index.cc +++ b/src/index/index.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -470,6 +471,48 @@ static auto index_main(const std::string_view &program, concurrency); } + ///////////////////////////////////////////////////////////////////////////// + // (12) Generate the pre computed routes + ///////////////////////////////////////////////////////////////////////////// + + sourcemeta::core::URITemplateRouter router; + router.add("/self/v1/api/list", sourcemeta::one::HANDLER_SELF_V1_API_LIST); + router.add("/self/v1/api/list/{+path}", + sourcemeta::one::HANDLER_SELF_V1_API_LIST_PATH); + router.add("/self/v1/api/schemas/dependencies/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_DEPENDENCIES); + router.add("/self/v1/api/schemas/health/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_HEALTH); + router.add("/self/v1/api/schemas/locations/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_LOCATIONS); + router.add("/self/v1/api/schemas/positions/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_POSITIONS); + router.add("/self/v1/api/schemas/stats/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_STATS); + router.add("/self/v1/api/schemas/metadata/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_METADATA); + router.add("/self/v1/api/schemas/evaluate/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_EVALUATE); + router.add("/self/v1/api/schemas/trace/{+schema}", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_TRACE); + router.add("/self/v1/api/schemas/search", + sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_SEARCH); + router.add("/self/v1/api/{+any}", + sourcemeta::one::HANDLER_SELF_V1_API_DEFAULT); + + if (configuration.html.has_value()) { + router.add("/self/static/{+path}", sourcemeta::one::HANDLER_SELF_STATIC); + } + + const auto routes_path{output.path() / "routes.bin"}; + DISPATCH( + routes_path, {mark_configuration_path, mark_version_path}, router, mutex, + "Producing", routes_path.string(), "routes", adapter, output); + + ///////////////////////////////////////////////////////////////////////////// + // Finish generation + ///////////////////////////////////////////////////////////////////////////// + // TODO: Print the size of the output directory here output.remove_unknown_files(); diff --git a/src/server/server.cc b/src/server/server.cc index 7dfc32ee..a0abd628 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -246,7 +246,7 @@ static const Handler HANDLERS[] = {handle_default, handle_self_api_not_found, handle_self_static}; -static auto dispatch(const sourcemeta::core::URITemplateRouter &router, +static auto dispatch(const sourcemeta::core::URITemplateRouterView &router, const std::filesystem::path &base, uWS::HttpResponse *const raw_response, uWS::HttpRequest *const raw_request) noexcept -> void { @@ -306,38 +306,7 @@ auto main(int argc, char *argv[]) noexcept -> int { const auto port{static_cast(std::stoul(argv[2]))}; const auto base{std::filesystem::canonical(argv[1])}; - const auto is_headless{!std::filesystem::exists( - base / "explorer" / SENTINEL / "directory-html.metapack")}; - - // TODO: Restore this from a URI Template binary view - sourcemeta::core::URITemplateRouter router; - router.add("/self/v1/api/list", sourcemeta::one::HANDLER_SELF_V1_API_LIST); - router.add("/self/v1/api/list/{+path}", - sourcemeta::one::HANDLER_SELF_V1_API_LIST_PATH); - router.add("/self/v1/api/schemas/dependencies/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_DEPENDENCIES); - router.add("/self/v1/api/schemas/health/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_HEALTH); - router.add("/self/v1/api/schemas/locations/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_LOCATIONS); - router.add("/self/v1/api/schemas/positions/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_POSITIONS); - router.add("/self/v1/api/schemas/stats/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_STATS); - router.add("/self/v1/api/schemas/metadata/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_METADATA); - router.add("/self/v1/api/schemas/evaluate/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_EVALUATE); - router.add("/self/v1/api/schemas/trace/{+schema}", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_TRACE); - router.add("/self/v1/api/schemas/search", - sourcemeta::one::HANDLER_SELF_V1_API_SCHEMAS_SEARCH); - router.add("/self/v1/api/{+any}", - sourcemeta::one::HANDLER_SELF_V1_API_DEFAULT); - - if (!is_headless) { - router.add("/self/static/{+path}", sourcemeta::one::HANDLER_SELF_STATIC); - } + const sourcemeta::core::URITemplateRouterView router{base / "routes.bin"}; uWS::LocalCluster( {}, [&router, &base, port, timestamp_start](uWS::SSLApp &app) -> void { diff --git a/test/cli/index/rebuild-cache.sh b/test/cli/index/rebuild-cache.sh index 931dc215..67ece238 100755 --- a/test/cli/index/rebuild-cache.sh +++ b/test/cli/index/rebuild-cache.sh @@ -99,6 +99,7 @@ Detecting: $(realpath "$TMP")/schemas/foo.json (#1) (skip) Rendering: . [not-found] (100%) Rendering: example/schemas/foo (skip) Rendering: example/schemas/foo [schema] +(skip) Producing: $(realpath "$TMP")/output/routes.bin [routes] EOF diff "$TMP/output.txt" "$TMP/expected.txt" @@ -128,6 +129,7 @@ Detecting: $(realpath "$TMP")/schemas/foo.json (#1) ( 75%) Rendering: . (skip) Rendering: . [not-found] (100%) Rendering: example/schemas/foo +(skip) Producing: $(realpath "$TMP")/output/routes.bin [routes] EOF diff "$TMP/output.txt" "$TMP/expected.txt" diff --git a/test/cli/index/rebuild-to-empty.sh b/test/cli/index/rebuild-to-empty.sh index b00287c2..682f5fa9 100755 --- a/test/cli/index/rebuild-to-empty.sh +++ b/test/cli/index/rebuild-to-empty.sh @@ -58,6 +58,8 @@ cat << 'EOF' > "$TMP/new-expected.txt" ./explorer/%/directory.metapack.deps ./explorer/%/search.metapack ./explorer/%/search.metapack.deps +./routes.bin +./routes.bin.deps ./version.json EOF diff --git a/test/sandbox/manifest-empty.txt b/test/sandbox/manifest-empty.txt index 4d66a403..6536f32e 100644 --- a/test/sandbox/manifest-empty.txt +++ b/test/sandbox/manifest-empty.txt @@ -9,4 +9,6 @@ ./explorer/%/directory.metapack.deps ./explorer/%/search.metapack ./explorer/%/search.metapack.deps +./routes.bin +./routes.bin.deps ./version.json diff --git a/test/sandbox/manifest-headless.txt b/test/sandbox/manifest-headless.txt index 6c714f7e..74467f22 100644 --- a/test/sandbox/manifest-headless.txt +++ b/test/sandbox/manifest-headless.txt @@ -5399,6 +5399,8 @@ ./explorer/test/v2.0/schema/% ./explorer/test/v2.0/schema/%/schema.metapack ./explorer/test/v2.0/schema/%/schema.metapack.deps +./routes.bin +./routes.bin.deps ./schemas ./schemas/self ./schemas/self/v1 diff --git a/test/sandbox/manifest-html.txt b/test/sandbox/manifest-html.txt index 54088b27..3210a5a6 100644 --- a/test/sandbox/manifest-html.txt +++ b/test/sandbox/manifest-html.txt @@ -8101,6 +8101,8 @@ ./explorer/test/v2.0/schema/%/schema-html.metapack.deps ./explorer/test/v2.0/schema/%/schema.metapack ./explorer/test/v2.0/schema/%/schema.metapack.deps +./routes.bin +./routes.bin.deps ./schemas ./schemas/self ./schemas/self/v1