Removed static members from application.

This commit is contained in:
Relintai 2021-04-28 22:58:13 +02:00
parent 614a496b7d
commit 8c483b4f8e
4 changed files with 18 additions and 28 deletions

View File

@ -22,7 +22,7 @@ void Application::setup_routes() {
} }
void Application::setup_middleware() { 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) { void Application::default_routing_middleware(Object *instance, Request *request) {
@ -136,15 +136,5 @@ Application::~Application() {
middlewares.clear(); middlewares.clear();
} }
HandlerInstance Application::index_func;
std::map<std::string, HandlerInstance> Application::main_route_map;
std::vector<HandlerInstance> Application::middlewares;
std::map<int, std::function<void(int, Request *)> > Application::error_handler_map;
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
std::string Application::default_error_404_body = "<html><body>404 :(</body></html>"; std::string Application::default_error_404_body = "<html><body>404 :(</body></html>";
std::string Application::default_generic_error_body = "<html><body>Internal server error! :(</body></html>"; std::string Application::default_generic_error_body = "<html><body>Internal server error! :(</body></html>";
std::mutex Application::_update_registered_requests_mutex;
std::vector<Request *> Application::_update_registered_requests;

View File

@ -18,9 +18,9 @@ public:
static std::string default_error_404_body; static std::string default_error_404_body;
static std::string default_generic_error_body; static std::string default_generic_error_body;
static void handle_request(Request *request); void handle_request(Request *request);
static void send_error(int error_code, Request *request); void send_error(int error_code, Request *request);
static void send_file(const std::string &path, 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_fallback_error_handler(int error_code, Request *request);
static void default_404_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_routes();
virtual void setup_middleware(); virtual void setup_middleware();
static void default_routing_middleware(Object *instance, Request *request); void default_routing_middleware(Object *instance, Request *request);
virtual void migrate(); virtual void migrate();
static void register_request_update(Request *request); void register_request_update(Request *request);
static void unregister_request_update(Request *request); void unregister_request_update(Request *request);
static void update(); void update();
Application(); Application();
virtual ~Application(); virtual ~Application();
public: public:
static HandlerInstance index_func; HandlerInstance index_func;
static std::map<std::string, HandlerInstance> main_route_map; std::map<std::string, HandlerInstance> main_route_map;
static std::vector<HandlerInstance> middlewares; std::vector<HandlerInstance> middlewares;
static std::map<int, std::function<void(int, Request *)> > error_handler_map; std::map<int, std::function<void(int, Request *)> > error_handler_map;
static std::function<void(int, Request *)> default_error_handler_func; std::function<void(int, Request *)> default_error_handler_func;
protected: protected:
static std::mutex _update_registered_requests_mutex; std::mutex _update_registered_requests_mutex;
static std::vector<Request *> _update_registered_requests; std::vector<Request *> _update_registered_requests;
}; };
#endif #endif

View File

@ -6,7 +6,7 @@
#define LOG_VERBOSE 0 #define LOG_VERBOSE 0
void HTTPServer::http_callback_handler(Request *request) { 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) { void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const HttpSession::Ptr &session) {

View File

@ -89,7 +89,7 @@ void Request::send_file(const std::string &p_file_path) {
response->addHeadValue("Connection", "Close"); response->addHeadValue("Connection", "Close");
std::string result = "HTTP/1.1 200 OK\r\nConnection: Close\r\n\r\n"; 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(); }); session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); });
} }
@ -233,7 +233,7 @@ void Request::_progress_send_file() {
if (!f) { if (!f) {
printf("Error: Download: In progress file doesn't exists anymore! %s\n", file_path.c_str()); 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(); session->postShutdown();