From 65f7f3a6b2823a8817a81b5652e77bf3f32bcd7f Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 1 Dec 2020 12:56:04 +0100 Subject: [PATCH] Now handlers need to have a new Object* parameter, this can be used to optionally carry class instances. (think of it as self in python object methods, except it can be null). --- core/application.cpp | 23 +++++++++++++---------- core/application.h | 31 ++++++++++++++++++------------- core/handler_instance.cpp | 13 +++++++++++++ core/handler_instance.h | 17 +++++++++++++++++ core/object.cpp | 1 + core/object.h | 12 ++++++++++++ core/request.cpp | 6 ++++-- core/request.h | 6 ++++-- rdn_application.cpp | 10 ++++++---- rdn_application.h | 5 +++-- 10 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 core/handler_instance.cpp create mode 100644 core/handler_instance.h create mode 100644 core/object.cpp create mode 100644 core/object.h diff --git a/core/application.cpp b/core/application.cpp index 664f645..d785c33 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -19,10 +19,10 @@ void Application::setup_routes() { } void Application::setup_middleware() { - middlewares.push_back(Application::default_routing_middleware); + middlewares.push_back(HandlerInstance(Application::default_routing_middleware)); } -void Application::default_routing_middleware(Request *request) { +void Application::default_routing_middleware(Object *instance, Request *request) { std::string path = request->http_parser->getPath(); if (FileCache::get_singleton()->wwwroot_has_file(path)) { @@ -31,11 +31,13 @@ void Application::default_routing_middleware(Request *request) { return; } - std::function func; + HandlerInstance handler_data; + + //std::function func; if (path == "/") { //quick shortcut - func = index_func; + handler_data = index_func; } else { std::string main_route = ""; @@ -48,16 +50,16 @@ void Application::default_routing_middleware(Request *request) { main_route = path.substr(1, endpos - 1); - func = main_route_map[main_route]; + handler_data = main_route_map[main_route]; } - if (!func) { + if (!handler_data.handler_func) { send_error(404, request); return; } - request->handler_func = func; + request->handler_instance = handler_data; request->next_stage(); } @@ -131,11 +133,12 @@ Application *Application::get_instance() { return _instance; } -std::function Application::index_func = nullptr; -std::map > Application::main_route_map; +HandlerInstance Application::index_func; +std::map Application::main_route_map; +std::vector Application::middlewares; + std::map > Application::error_handler_map; std::function Application::default_error_handler_func = nullptr; -std::vector > Application::middlewares; Application *Application::_instance = nullptr; diff --git a/core/application.h b/core/application.h index 96f6a29..94780a8 100644 --- a/core/application.h +++ b/core/application.h @@ -1,40 +1,45 @@ #ifndef APPLICATION_H #define APPLICATION_H +#include "object.h" #include #include #include #include +#include "handler_instance.h" + class Request; class Application { public: - static std::string default_error_404_body; - static std::string default_generic_error_body; + static std::string default_error_404_body; + static std::string default_generic_error_body; static void handle_request(Request *request); - static void send_error(int error_code, Request *request); - static void send_file(const std::string &path, Request *request); + static void send_error(int error_code, Request *request); + static void send_file(const std::string &path, Request *request); - static void default_fallback_error_handler(int error_code, Request *request); - static void default_404_error_handler(int error_code, Request *request); + static void default_fallback_error_handler(int error_code, Request *request); + static void default_404_error_handler(int error_code, Request *request); virtual void setup_routes(); - virtual void setup_middleware(); + virtual void setup_middleware(); - static void default_routing_middleware(Request *request); + static void default_routing_middleware(Object *instance, Request *request); Application(); virtual ~Application(); - static Application *get_instance(); + static Application *get_instance(); + +public: + static HandlerInstance index_func; + static std::map main_route_map; + static std::vector middlewares; - static std::function index_func; - static std::map > main_route_map; static std::map > error_handler_map; - static std::function default_error_handler_func; - static std::vector > middlewares; + static std::function default_error_handler_func; private: static Application *_instance; diff --git a/core/handler_instance.cpp b/core/handler_instance.cpp new file mode 100644 index 0000000..e1c9288 --- /dev/null +++ b/core/handler_instance.cpp @@ -0,0 +1,13 @@ +#include "handler_instance.h" + +#include "request.h" +#include "object.h" + +HandlerInstance::HandlerInstance() { + instance = nullptr; +} + +HandlerInstance::HandlerInstance(std::function p_handler_func, Object *p_instance) { + handler_func = p_handler_func; + instance = p_instance; +} \ No newline at end of file diff --git a/core/handler_instance.h b/core/handler_instance.h new file mode 100644 index 0000000..0451cf1 --- /dev/null +++ b/core/handler_instance.h @@ -0,0 +1,17 @@ +#ifndef HANDLER_INSTANCE_H +#define HANDLER_INSTANCE_H + +#include + +class Object; +class Request; + +struct HandlerInstance { + std::function handler_func; + Object *instance; + + HandlerInstance(); + HandlerInstance(std::function p_handler_func, Object *p_instance = nullptr); +}; + +#endif \ No newline at end of file diff --git a/core/object.cpp b/core/object.cpp new file mode 100644 index 0000000..d52452b --- /dev/null +++ b/core/object.cpp @@ -0,0 +1 @@ +#include "object.h" \ No newline at end of file diff --git a/core/object.h b/core/object.h new file mode 100644 index 0000000..850c055 --- /dev/null +++ b/core/object.h @@ -0,0 +1,12 @@ +#ifndef OBJECT_H +#define OBJECT_H + +class Object { +public: + + Object(); + virtual ~Object(); +}; + + +#endif \ No newline at end of file diff --git a/core/request.cpp b/core/request.cpp index 56129f0..599d7d3 100644 --- a/core/request.cpp +++ b/core/request.cpp @@ -2,11 +2,13 @@ void Request::next_stage() { if (current_middleware_index == (*middleware_stack).size()) { - handler_func(this); + handler_instance.handler_func(handler_instance.instance, this); return; } - (*middleware_stack)[current_middleware_index++](this); + const HandlerInstance &hi = (*middleware_stack)[current_middleware_index++]; + + hi.handler_func(hi.instance, this); } void Request::send() { diff --git a/core/request.h b/core/request.h index 2d46591..43e1c2e 100644 --- a/core/request.h +++ b/core/request.h @@ -7,6 +7,8 @@ #include #include +#include "handler_instance.h" + using namespace brynet; using namespace brynet::net; using namespace brynet::net::http; @@ -18,8 +20,8 @@ public: HttpResponse *response; uint32_t current_middleware_index; - std::function handler_func; - std::vector > *middleware_stack; + HandlerInstance handler_instance; + std::vector *middleware_stack; void next_stage(); void send(); diff --git a/rdn_application.cpp b/rdn_application.cpp index 68ae951..55b2223 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -6,7 +6,9 @@ #include "core/file_cache.h" -void RDNApplication::index(Request *request) { +#include "core/handler_instance.h" + +void RDNApplication::index(Object *instance, Request *request) { std::string body; if (FileCache::get_singleton()->get_cached_body("index", &body)) { @@ -23,7 +25,7 @@ void RDNApplication::index(Request *request) { request->send(); } -void RDNApplication::session_middleware_func(Request *request) { +void RDNApplication::session_middleware_func(Object* instance, Request *request) { std::cout << "test: session_middleware_func called" << std::endl; //if fail @@ -35,9 +37,9 @@ void RDNApplication::session_middleware_func(Request *request) { void RDNApplication::setup_routes() { Application::setup_routes(); - index_func = index; + index_func = HandlerInstance(index); - main_route_map["asd"] = index; + main_route_map["asd"] = HandlerInstance(index); } void RDNApplication::setup_middleware() { diff --git a/rdn_application.h b/rdn_application.h index 6754233..5f2921f 100644 --- a/rdn_application.h +++ b/rdn_application.h @@ -3,12 +3,13 @@ #include "core/application.h" #include "core/theme.h" +#include "core/object.h" class RDNApplication : public Application { public: - static void index(Request *request); + static void index(Object *instance, Request *request); - static void session_middleware_func(Request *request); + static void session_middleware_func(Object* instance, Request *request); virtual void setup_routes(); virtual void setup_middleware();