diff --git a/core/application.cpp b/core/application.cpp index e23f9dd..ba43bc4 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -128,7 +128,6 @@ void Application::update() { } Application::Application() { - _instance = this; } Application::~Application() { @@ -137,10 +136,6 @@ Application::~Application() { middlewares.clear(); } -Application *Application::get_instance() { - return _instance; -} - HandlerInstance Application::index_func; std::map Application::main_route_map; std::vector Application::middlewares; @@ -148,8 +143,6 @@ std::vector Application::middlewares; std::map > Application::error_handler_map; std::function Application::default_error_handler_func = nullptr; -Application *Application::_instance = nullptr; - std::string Application::default_error_404_body = "404 :("; std::string Application::default_generic_error_body = "Internal server error! :("; diff --git a/core/application.h b/core/application.h index 578acc0..1351b9d 100644 --- a/core/application.h +++ b/core/application.h @@ -40,8 +40,6 @@ public: Application(); virtual ~Application(); - static Application *get_instance(); - public: static HandlerInstance index_func; static std::map main_route_map; @@ -53,9 +51,6 @@ public: protected: static std::mutex _update_registered_requests_mutex; static std::vector _update_registered_requests; - -private: - static Application *_instance; }; #endif \ No newline at end of file diff --git a/core/http_server.cpp b/core/http_server.cpp index 6d1cc72..fe6ee9c 100644 --- a/core/http_server.cpp +++ b/core/http_server.cpp @@ -20,6 +20,7 @@ void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const Ht _request_map[s] = request; lock.unlock(); + request->application = application; request->http_parser = std::make_shared(httpParser); request->session = session; @@ -117,7 +118,7 @@ void HTTPServer::main_loop() { break; } - Application::get_instance()->update(); + application->update(); } } @@ -125,6 +126,8 @@ HTTPServer::HTTPServer() { port = 80; threads = 4; listenBuilder = nullptr; + + application = nullptr; } HTTPServer::~HTTPServer() { diff --git a/core/http_server.h b/core/http_server.h index b9f993f..d49faca 100644 --- a/core/http_server.h +++ b/core/http_server.h @@ -19,6 +19,7 @@ using namespace brynet::net; using namespace brynet::net::http; class Request; +class Application; class HTTPServer { public: @@ -41,6 +42,9 @@ public: HTTPServer(); virtual ~HTTPServer(); + //move this to a sublcass + Application *application; + protected: std::map _request_map; std::mutex _request_map_mutex; diff --git a/core/request.cpp b/core/request.cpp index b013946..ec57d52 100644 --- a/core/request.cpp +++ b/core/request.cpp @@ -94,7 +94,12 @@ void Request::send_file(const std::string &p_file_path) { session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); }); } +void Request::send_error(int error_code) { + application->send_error(error_code, this); +} + void Request::reset() { + application = nullptr; http_parser = nullptr; session = nullptr; current_middleware_index = 0; diff --git a/core/request.h b/core/request.h index de89746..b8304c2 100644 --- a/core/request.h +++ b/core/request.h @@ -13,11 +13,14 @@ using namespace brynet; using namespace brynet::net; using namespace brynet::net::http; +class Application; + class Request { public: HTTPParser::Ptr http_parser; HttpSession::Ptr session; HttpResponse *response; + Application *application; uint32_t current_middleware_index; HandlerInstance handler_instance; @@ -41,6 +44,7 @@ public: void next_stage(); void send(); void send_file(const std::string &p_file_path); + void send_error(int error_code); void reset(); void setup_url_stack(); diff --git a/modules/paged_article/paged_article.cpp b/modules/paged_article/paged_article.cpp index 0658435..d043f9c 100644 --- a/modules/paged_article/paged_article.cpp +++ b/modules/paged_article/paged_article.cpp @@ -16,7 +16,7 @@ void PagedArticle::index(Request *request) { Article *s = pages[r]; if (s == nullptr) { - Application::get_instance()->send_error(404, request); + request->send_error(404); return; } @@ -48,7 +48,7 @@ void PagedArticle::index(Request *request) { if (page == nullptr) { //bad url - Application::get_instance()->send_error(404, request); + request->send_error(404); return; }