#pragma once #include "Route.h" #include #include namespace VWeb { typedef std::function RouteFunction; typedef std::function()> RouteInstaniateFunction; class Router { public: Router(); void DeleteRoute(const std::string &name); Ref HandleRoute(Ref &request); Ref FindRoute(Ref &request); static void AddToArgs(Ref &request, std::vector &items); 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"); allowedMethods |= HttpMethod::HEAD | HttpMethod::OPTIONS; m_Routes[endpoint] = {.AllowedMethods = allowedMethods, .Instaniate = [] { return std::make_shared(); }}; } void Get(const std::string &path, RouteFunction); void Post(const std::string &path, RouteFunction); void Put(const std::string &path, RouteFunction); void Patch(const std::string &path, RouteFunction); void Delete(const std::string &path, RouteFunction); private: struct RouteInstance { uint32_t AllowedMethods = HttpMethod::OPTIONS | HttpMethod::HEAD; RouteInstaniateFunction Instaniate; }; std::unordered_map m_Routes; std::unordered_map m_FunctionRoutes; }; } // namespace VWeb