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

86 lines
1.6 KiB
C++

#include <seafire/routing/routing-table.hxx>
#include <seafire/routing/match-path.hxx>
#include <seafire/routing/flatten-route.hxx>
namespace seafire::routing
{
routing_table_t::
routing_table_t(std::vector<endpoint_t> endpoints)
: endpoints_{std::move(endpoints)}
{}
std::vector<endpoint_t> const&
routing_table_t::
endpoints() const
{
return endpoints_;
}
std::optional<routing_table_t::find_result_t>
routing_table_t::
find_route(std::string const& path) const
{
for (auto const& e : endpoints()) {
route_parameters_t params;
if (match_path(e.pattern(), path, params))
return find_result_t{e.handler(), std::move(params)};
}
return {};
}
routing_table_t::builder_t::
builder_t() = default;
routing_table_t::builder_t::
~builder_t() noexcept = default;
route_t&
routing_table_t::builder_t::
add_route()
{
roots_.emplace_back();
return roots_.back();
}
route_t&
routing_table_t::builder_t::
add_route(route_t r)
{
roots_.emplace_back(std::move(r));
return roots_.back();
}
route_t&
routing_table_t::builder_t::
add_route(std::string path)
{
return add_route(route_t{std::move(path)});
}
route_t&
routing_table_t::builder_t::
add_route(std::string path,
server::request_handler_t handler)
{
return add_route({std::move(path), std::move(handler)});
}
routing_table_t
routing_table_t::builder_t::
build() const
{
std::vector<endpoint_t> endpoints;
for (auto const& r : roots_) {
flatten_route(endpoints, r);
}
return routing_table_t{std::move(endpoints)};
}
} // namespace seafire::routing