Seafire-Routing/seafire/routing/flatten-route.cxx
2025-03-08 20:55:07 +01:00

51 lines
1.2 KiB
C++

#include <seafire/routing/flatten-route.hxx>
namespace seafire::routing
{
void
flatten_route(std::vector<endpoint_t>& endpoints,
std::vector<server::middleware_t> middlewares,
route_t const& route,
std::string const& root)
{
auto const path = root + route.path();
// append any middlewares if we have any.
//
for (auto const& m : route.middleware()) {
middlewares.emplace_back(m);
}
// generate an endpoint for this route if we have a handler.
//
if (auto const& h = route.handler()) {
endpoints.emplace_back(path, server::make_middleware(middlewares, *h));
}
// flatten any child routes.
//
for (auto const& child_route : route.children()) {
flatten_route(endpoints,
middlewares,
child_route,
route.path().empty() ? path : path + '/');
}
}
void
flatten_route(std::vector<endpoint_t>& endpoints, route_t const& r)
{
flatten_route(endpoints, {}, r, "/");
}
std::vector<endpoint_t>
flatten_route(route_t const& r)
{
std::vector<endpoint_t> endpoints;
flatten_route(endpoints, r);
return endpoints;
}
} // namespace seafire::routing