26 template <FixedString path>
28 const RouteCallback<path, ContextType>& callback,
29 Method::HttpMethod method
31 std::shared_ptr<BaseRoute<ContextType>> route = std::make_shared<Route<path, ContextType>>(
34 auto splitPath = pathToComponents(path.c_str());
36 this->routes.pushRoute(route, method, splitPath);
40 constexpr void normalisePath(
41 const std::string_view& path,
42 std::string_view& normPath,
43 std::string_view& rawGetArgs
45 if (
auto pos = path.find_first_of(
'?'); pos != std::string::npos) {
46 rawGetArgs = path.substr(pos);
47 normPath = path.substr(0, pos);
53 constexpr std::vector<std::string_view> pathToComponents(
const std::string_view& path)
const {
56 std::vector<std::string_view> out {
59 while ((next = path.find(
'/', start + 1)) != std::string::npos) {
60 if (next - start == 1) {
62 }
else if (next != start) {
63 out.push_back(path.substr(start + 1, (next - start - 1)));
70 if (start < path.size() - 1) {
71 out.push_back(path.substr(start + 1));
78 const std::string& path,
85 if (path.size() == 0 || path[0] !=
'/') {
87 throw std::runtime_error(
"Invalid route supplied");
90 std::string_view normPath;
91 std::string_view rawGetArgs;
92 normalisePath(path, normPath, rawGetArgs);
93 auto segments = this->pathToComponents(normPath);
98 std::visit([&res, &req, ctx, &segments](
auto& it) {
99 using T = std::decay_t<
decltype(it)>;
101 auto* castCtx =
static_cast<ContextType*
>(ctx);
102 if constexpr (std::is_same_v<dsa::FindError, T>) {
103 contextApp->notFoundErrorHandler->onRouteNotFound(castCtx, req, res, it);
105 contextApp->errorHandler->tryCall(
106 castCtx, req, res, [&]() {
107 auto* route = it.get();
111 contextApp->getMiddlewaresAsPtr(),
112 route->getMiddlewaresAsPtr(),
Definition AppDecl.hpp:37
std::variant< FindError, Value > getRoute(const std::vector< std::string_view > &route, Method::HttpMethod method) const
Definition RadixTree.hpp:131