Skip to content

Commit

Permalink
Little refactor to simplify mounting of api_routers into an API follo…
Browse files Browse the repository at this point in the history
…wing the IS-04 'child resources' (sub-route) URL and response behaviours

(cherry picked from commit e5ed4742fa7adc733ec0e9069e6f4c8999e931e0)
  • Loading branch information
garethsb committed Nov 15, 2019
1 parent f49183d commit 22adc72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
30 changes: 16 additions & 14 deletions Development/nmos/admin_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,34 @@ namespace nmos
{
namespace experimental
{
namespace patterns
{
const route_pattern admin_ui = make_route_pattern(U("api"), U("admin"));
}

web::http::experimental::listener::api_router make_admin_ui(const utility::string_t& filesystem_root, slog::base_gate& gate)
web::http::experimental::listener::api_router make_api_sub_route(const utility::string_t& sub_route, web::http::experimental::listener::api_router sub_router, slog::base_gate& gate)
{
using namespace web::http::experimental::listener::api_router_using_declarations;

api_router admin_ui;
api_router router;

const route_pattern api = make_route_pattern(U("api"), sub_route);

admin_ui.support(U("/?"), methods::GET, [](http_request req, http_response res, const string_t&, const route_parameters&)
router.support(U("/?"), methods::GET, [sub_route](http_request req, http_response res, const string_t&, const route_parameters&)
{
set_reply(res, status_codes::OK, nmos::make_sub_routes_body({ U("admin/") }, req, res));
set_reply(res, status_codes::OK, nmos::make_sub_routes_body({ sub_route + U('/') }, req, res));
return pplx::task_from_result(true);
});

admin_ui.support(U("/") + nmos::experimental::patterns::admin_ui.pattern, methods::GET, [](http_request, http_response res, const string_t&, const route_parameters&)
router.support(U("/") + api.pattern, methods::GET, [](http_request req, http_response res, const string_t&, const route_parameters&)
{
set_reply(res, status_codes::TemporaryRedirect); // or status_codes::MovedPermanently?
res.headers().add(web::http::header_names::location, U("/admin/"));
res.headers().add(web::http::header_names::location, req.request_uri().path() + U('/'));
return pplx::task_from_result(true);
});

router.mount(U("/") + api.pattern + U("(?=.)"), web::http::methods::GET, std::move(sub_router));

return router;
}

web::http::experimental::listener::api_router make_admin_ui(const utility::string_t& filesystem_root, slog::base_gate& gate)
{
// To serve the admin UI, only a few HTML, JavaScript and CSS files are necessary
const nmos::experimental::content_types valid_extensions =
{
Expand All @@ -43,9 +47,7 @@ namespace nmos
{ U("png"), U("image/png") }
};

admin_ui.mount(U("/") + nmos::experimental::patterns::admin_ui.pattern, web::http::methods::GET, nmos::experimental::make_filesystem_route(filesystem_root, nmos::experimental::make_relative_path_content_type_handler(valid_extensions), gate));

return admin_ui;
return nmos::experimental::make_api_sub_route(U("admin"), nmos::experimental::make_filesystem_route(filesystem_root, nmos::experimental::make_relative_path_content_type_handler(valid_extensions), gate), gate);
}
}
}
3 changes: 3 additions & 0 deletions Development/nmos/admin_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace nmos
{
namespace experimental
{
// (could be moved to nmos/api_utils.h or its own file?)
web::http::experimental::listener::api_router make_api_sub_route(const utility::string_t& sub_route, web::http::experimental::listener::api_router sub_router, slog::base_gate& gate);

web::http::experimental::listener::api_router make_admin_ui(const utility::string_t& filesystem_root, slog::base_gate& gate);
}
}
Expand Down

0 comments on commit 22adc72

Please sign in to comment.