#pragma once #include "Route.h" #include #include namespace VWeb { typedef std::function RouteFunction; typedef std::function()> RouteInstaniateFunction; struct RouteTree { void Add(const std::string &path, uint32_t allowedMethods, RouteInstaniateFunction instaniate); Ref Find(const std::string &path, Request &request); protected: struct Node { explicit Node(const uint64_t id) : ID(id) {} std::unordered_map> Children{}; uint64_t ID{0}; }; Node Root{0}; uint64_t m_NodeID = 1; struct RouteInstance { uint32_t AllowedMethods = HttpMethod::OPTIONS | HttpMethod::HEAD; RouteInstaniateFunction Instaniate; }; std::unordered_map m_Routes; }; class Router { public: Router(); Ref HandleRoute(Ref &request); Ref FindRoute(Ref &request); template void Register(const std::string &endpoint, HttpMethod allowedMethod) { Register(endpoint, static_cast(allowedMethod)); } template void Register(const std::string &endpoint, uint32_t allowedMethods = static_cast(HttpMethod::ALL)) { static_assert(std::is_base_of_v, "must be a Route"); m_Tree.Add(endpoint, allowedMethods | HttpMethod::HEAD | HttpMethod::OPTIONS, [] { return std::make_shared(); }); } void Get(const std::string &path, const RouteFunction &); void Post(const std::string &path, const RouteFunction &); void Put(const std::string &path, const RouteFunction &); void Patch(const std::string &path, const RouteFunction &); void Delete(const std::string &path, const RouteFunction &); private: RouteTree m_Tree{}; }; } // namespace VWeb