Router: Uses Tree and Use named Parameters

This commit is contained in:
Maurice Grönwoldt 2024-04-16 20:09:22 +02:00
commit 2bd34c2bb1
8 changed files with 139 additions and 122 deletions

View file

@ -29,6 +29,6 @@ public:
ParameterValue &Header(const std::string &key) { return Headers[key]; }
std::unordered_map<std::string, ParameterValue> Parameters;
std::unordered_map<std::string, ParameterValue> Headers;
std::vector<std::string> URLParameters;
std::unordered_map<std::string, std::string> URLParameters;
};
} // namespace VWeb

View file

@ -3,20 +3,38 @@
#include "Route.h"
#include <functional>
#include <vector>
#include <memory>
namespace VWeb {
typedef std::function<bool(Request &, Response &)> RouteFunction;
typedef std::function<std::shared_ptr<Route>()> RouteInstaniateFunction;
struct RouteTree {
void Add(const std::string &path, uint32_t allowedMethods,
RouteInstaniateFunction instaniate);
Ref<Route> Find(const std::string &path, Request &request);
protected:
struct Node {
explicit Node(const uint64_t id) : ID(id) {}
std::unordered_map<std::string, std::unique_ptr<Node>> 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<uint64_t, RouteInstance> m_Routes;
};
class Router {
public:
Router();
void DeleteRoute(const std::string &name);
Ref<Response> HandleRoute(Ref<Request> &request);
Ref<Route> FindRoute(Ref<Request> &request);
static void AddToArgs(Ref<Request> &request, std::vector<std::string> &items);
template <typename T>
void Register(const std::string &endpoint, HttpMethod allowedMethod) {
@ -28,23 +46,18 @@ public:
Register(const std::string &endpoint,
uint32_t allowedMethods = static_cast<uint32_t>(HttpMethod::ALL)) {
static_assert(std::is_base_of_v<Route, T>, "must be a Route");
allowedMethods |= HttpMethod::HEAD | HttpMethod::OPTIONS;
m_Routes[endpoint] = {.AllowedMethods = allowedMethods,
.Instaniate = [] { return std::make_shared<T>(); }};
m_Tree.Add(endpoint,
allowedMethods | HttpMethod::HEAD | HttpMethod::OPTIONS,
[] { return std::make_shared<T>(); });
}
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);
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:
struct RouteInstance {
uint32_t AllowedMethods = HttpMethod::OPTIONS | HttpMethod::HEAD;
RouteInstaniateFunction Instaniate;
};
std::unordered_map<std::string, RouteInstance> m_Routes;
std::unordered_map<std::string, RouteFunction> m_FunctionRoutes;
RouteTree m_Tree{};
};
} // namespace VWeb

View file

@ -21,7 +21,6 @@ public:
void Stop() { m_IsExit = true; }
Ref<Router> &GetRouter() { return m_Router; }
Ref<ServerConfig> &GetServerConfig() { return m_ServerConfig; }
void RemoveRoute(const std::string &path) const;
Ref<MiddleWareHandler> &Middleware();