Make Router more sexy
This commit is contained in:
parent
5bb68a7d02
commit
d547bb9f95
9 changed files with 144 additions and 173 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include <limits>
|
||||
|
||||
namespace VWeb {
|
||||
enum class HttpStatusCode : int {
|
||||
|
|
@ -63,14 +64,25 @@ enum class HttpStatusCode : int {
|
|||
NotExtended = 510,
|
||||
NetworkAuthenticationRequired = 511
|
||||
};
|
||||
enum class HttpMethod {
|
||||
GET = 0,
|
||||
HEAD,
|
||||
OPTIONS,
|
||||
POST,
|
||||
PUT,
|
||||
PATCH,
|
||||
DELETE,
|
||||
FALLBACK
|
||||
|
||||
enum class HttpMethod : uint32_t {
|
||||
GET = 1 << 0,
|
||||
HEAD = 1 << 1,
|
||||
OPTIONS = 1 << 2,
|
||||
POST = 1 << 3,
|
||||
PUT = 1 << 4,
|
||||
PATCH = 1 << 5,
|
||||
DELETE = 1 << 6,
|
||||
FALLBACK = 1 << 7,
|
||||
ALL = std::numeric_limits<uint32_t>::max()
|
||||
};
|
||||
|
||||
inline uint32_t operator|(HttpMethod lhs, HttpMethod rhs) {
|
||||
return static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs);
|
||||
}
|
||||
|
||||
inline bool operator&(const uint32_t lhs, HttpMethod rhs) {
|
||||
return lhs & static_cast<uint32_t>(rhs);
|
||||
}
|
||||
|
||||
} // namespace VWeb
|
||||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
#include "Cookie.h"
|
||||
#include "Http.h"
|
||||
#include "ParameterValue.h"
|
||||
#include "Session.h"
|
||||
#include "Types.h"
|
||||
#include "ParameterValue.h"
|
||||
|
||||
namespace VWeb {
|
||||
class Response {
|
||||
|
|
@ -22,7 +22,8 @@ public:
|
|||
std::string GetResponse();
|
||||
void SetHeader(const std::string &key, ParameterValue &value);
|
||||
void SetHeader(const std::string &key, const std::string &value);
|
||||
void AddHeaders(const std::string &key, const std::vector<std::string> &values);
|
||||
void AddHeaders(const std::string &key,
|
||||
const std::vector<std::string> &values);
|
||||
void AddHeader(const std::string &key, const std::string &value);
|
||||
void SetType(const std::string &type);
|
||||
void AddContent(const std::string &data);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "Http.h"
|
||||
#include <initializer_list>
|
||||
#include "Request.h"
|
||||
#include "Response.h"
|
||||
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
namespace VWeb {
|
||||
|
|
@ -12,23 +13,20 @@ class Route {
|
|||
public:
|
||||
Route() = default;
|
||||
virtual ~Route() = default;
|
||||
Route(std::initializer_list<HttpMethod>);
|
||||
virtual bool Execute(Request &request, Response &response);
|
||||
virtual bool Get(Request &request, Response &response);
|
||||
virtual bool Post(Request &request, Response &response);
|
||||
virtual bool Put(Request &request, Response &response);
|
||||
virtual bool Patch(Request &request, Response &response);
|
||||
virtual bool Delete(Request &request, Response &response);
|
||||
bool Options(Request &request, Response &response);
|
||||
virtual bool Fallback(Request &request, Response &response);
|
||||
bool SupportsMethod(Request &request);
|
||||
virtual bool IsAllowed(Request &request);
|
||||
|
||||
void AllowMethod(HttpMethod method);
|
||||
void SetAllowedMethods(uint32_t methods);
|
||||
[[nodiscard]] uint32_t GetAllowedMethods() const;
|
||||
|
||||
protected:
|
||||
bool m_AllowAll{true};
|
||||
std::vector<HttpMethod> m_AllowedMethods;
|
||||
uint32_t m_AllowedMethods = 0;
|
||||
friend Router;
|
||||
};
|
||||
}
|
||||
} // namespace VWeb
|
||||
|
|
@ -7,23 +7,44 @@
|
|||
|
||||
namespace VWeb {
|
||||
typedef std::function<bool(Request &, Response &)> RouteFunction;
|
||||
typedef std::function<std::shared_ptr<Route>()> RouteInstaniateFunction;
|
||||
|
||||
class Router {
|
||||
public:
|
||||
Router();
|
||||
void AddRoute(const std::string &name, const Ref<Route> &route);
|
||||
Ref<Route> &GetRoute(const std::string &name);
|
||||
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);
|
||||
|
||||
public:
|
||||
template <typename T>
|
||||
void Register(const std::string &endpoint, HttpMethod allowedMethod) {
|
||||
Register<T>(endpoint, static_cast<uint32_t>(allowedMethod));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void
|
||||
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>(); }};
|
||||
}
|
||||
|
||||
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);
|
||||
std::unordered_map<std::string, Ref<Route>> m_Routes;
|
||||
|
||||
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;
|
||||
};
|
||||
} // namespace VWeb
|
||||
|
|
@ -21,8 +21,7 @@ public:
|
|||
void Stop() { m_IsExit = true; }
|
||||
Ref<Router> &GetRouter() { return m_Router; }
|
||||
Ref<ServerConfig> &GetServerConfig() { return m_ServerConfig; }
|
||||
void AddRoute(const std::string &path, const Ref<Route> &route);
|
||||
void RemoveRoute(const std::string &path);
|
||||
void RemoveRoute(const std::string &path) const;
|
||||
|
||||
Ref<MiddleWareHandler> &Middleware();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue