From 8c483b4f8ee02bebb9db275a262208254f0d34fc Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 28 Apr 2021 22:58:13 +0200 Subject: [PATCH] Removed static members from application. --- core/application.cpp | 12 +----------- core/application.h | 28 ++++++++++++++-------------- core/http_server.cpp | 2 +- core/request.cpp | 4 ++-- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/core/application.cpp b/core/application.cpp index ba43bc4..3a16c54 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -22,7 +22,7 @@ void Application::setup_routes() { } void Application::setup_middleware() { - middlewares.push_back(HandlerInstance(Application::default_routing_middleware)); + middlewares.push_back(HandlerInstance([this](Object *instance, Request *request){ this->default_routing_middleware(instance, request); })); } void Application::default_routing_middleware(Object *instance, Request *request) { @@ -136,15 +136,5 @@ Application::~Application() { middlewares.clear(); } -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::string Application::default_error_404_body = "404 :("; std::string Application::default_generic_error_body = "Internal server error! :("; - -std::mutex Application::_update_registered_requests_mutex; -std::vector Application::_update_registered_requests; diff --git a/core/application.h b/core/application.h index 1351b9d..108969b 100644 --- a/core/application.h +++ b/core/application.h @@ -18,9 +18,9 @@ public: 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); + void handle_request(Request *request); + void send_error(int error_code, Request *request); + 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); @@ -29,28 +29,28 @@ public: virtual void setup_routes(); virtual void setup_middleware(); - static void default_routing_middleware(Object *instance, Request *request); + void default_routing_middleware(Object *instance, Request *request); virtual void migrate(); - static void register_request_update(Request *request); - static void unregister_request_update(Request *request); - static void update(); + void register_request_update(Request *request); + void unregister_request_update(Request *request); + void update(); Application(); virtual ~Application(); public: - static HandlerInstance index_func; - static std::map main_route_map; - static std::vector middlewares; + HandlerInstance index_func; + std::map main_route_map; + std::vector middlewares; - static std::map > error_handler_map; - static std::function default_error_handler_func; + std::map > error_handler_map; + std::function default_error_handler_func; protected: - static std::mutex _update_registered_requests_mutex; - static std::vector _update_registered_requests; + std::mutex _update_registered_requests_mutex; + std::vector _update_registered_requests; }; #endif \ No newline at end of file diff --git a/core/http_server.cpp b/core/http_server.cpp index fe6ee9c..4394674 100644 --- a/core/http_server.cpp +++ b/core/http_server.cpp @@ -6,7 +6,7 @@ #define LOG_VERBOSE 0 void HTTPServer::http_callback_handler(Request *request) { - Application::handle_request(request); + application->handle_request(request); } void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const HttpSession::Ptr &session) { diff --git a/core/request.cpp b/core/request.cpp index ec57d52..639ccc7 100644 --- a/core/request.cpp +++ b/core/request.cpp @@ -89,7 +89,7 @@ void Request::send_file(const std::string &p_file_path) { response->addHeadValue("Connection", "Close"); std::string result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n"; - Application::register_request_update(this); + application->register_request_update(this); session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); }); } @@ -233,7 +233,7 @@ void Request::_progress_send_file() { if (!f) { printf("Error: Download: In progress file doesn't exists anymore! %s\n", file_path.c_str()); - Application::unregister_request_update(this); + application->unregister_request_update(this); session->postShutdown();