mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
Application is not a singleton anymore, as I think having multiple ones can be useful. HttpServer has an application pointer now (temporarily), it will be subclassed later.
This commit is contained in:
parent
c5ea34afdc
commit
614a496b7d
@ -128,7 +128,6 @@ void Application::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Application::Application() {
|
Application::Application() {
|
||||||
_instance = this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application() {
|
Application::~Application() {
|
||||||
@ -137,10 +136,6 @@ Application::~Application() {
|
|||||||
middlewares.clear();
|
middlewares.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Application *Application::get_instance() {
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
HandlerInstance Application::index_func;
|
HandlerInstance Application::index_func;
|
||||||
std::map<std::string, HandlerInstance> Application::main_route_map;
|
std::map<std::string, HandlerInstance> Application::main_route_map;
|
||||||
std::vector<HandlerInstance> Application::middlewares;
|
std::vector<HandlerInstance> Application::middlewares;
|
||||||
@ -148,8 +143,6 @@ std::vector<HandlerInstance> Application::middlewares;
|
|||||||
std::map<int, std::function<void(int, Request *)> > Application::error_handler_map;
|
std::map<int, std::function<void(int, Request *)> > Application::error_handler_map;
|
||||||
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
|
std::function<void(int, Request *)> Application::default_error_handler_func = nullptr;
|
||||||
|
|
||||||
Application *Application::_instance = 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>";
|
||||||
|
|
||||||
|
@ -40,8 +40,6 @@ public:
|
|||||||
Application();
|
Application();
|
||||||
virtual ~Application();
|
virtual ~Application();
|
||||||
|
|
||||||
static Application *get_instance();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static HandlerInstance index_func;
|
static HandlerInstance index_func;
|
||||||
static std::map<std::string, HandlerInstance> main_route_map;
|
static std::map<std::string, HandlerInstance> main_route_map;
|
||||||
@ -53,9 +51,6 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
static std::mutex _update_registered_requests_mutex;
|
static std::mutex _update_registered_requests_mutex;
|
||||||
static std::vector<Request *> _update_registered_requests;
|
static std::vector<Request *> _update_registered_requests;
|
||||||
|
|
||||||
private:
|
|
||||||
static Application *_instance;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -20,6 +20,7 @@ void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const Ht
|
|||||||
_request_map[s] = request;
|
_request_map[s] = request;
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
|
request->application = application;
|
||||||
request->http_parser = std::make_shared<HTTPParser>(httpParser);
|
request->http_parser = std::make_shared<HTTPParser>(httpParser);
|
||||||
request->session = session;
|
request->session = session;
|
||||||
|
|
||||||
@ -117,7 +118,7 @@ void HTTPServer::main_loop() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::get_instance()->update();
|
application->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +126,8 @@ HTTPServer::HTTPServer() {
|
|||||||
port = 80;
|
port = 80;
|
||||||
threads = 4;
|
threads = 4;
|
||||||
listenBuilder = nullptr;
|
listenBuilder = nullptr;
|
||||||
|
|
||||||
|
application = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPServer::~HTTPServer() {
|
HTTPServer::~HTTPServer() {
|
||||||
|
@ -19,6 +19,7 @@ using namespace brynet::net;
|
|||||||
using namespace brynet::net::http;
|
using namespace brynet::net::http;
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
class Application;
|
||||||
|
|
||||||
class HTTPServer {
|
class HTTPServer {
|
||||||
public:
|
public:
|
||||||
@ -41,6 +42,9 @@ public:
|
|||||||
HTTPServer();
|
HTTPServer();
|
||||||
virtual ~HTTPServer();
|
virtual ~HTTPServer();
|
||||||
|
|
||||||
|
//move this to a sublcass
|
||||||
|
Application *application;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::map<HttpSession *, Request *> _request_map;
|
std::map<HttpSession *, Request *> _request_map;
|
||||||
std::mutex _request_map_mutex;
|
std::mutex _request_map_mutex;
|
||||||
|
@ -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(); });
|
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() {
|
void Request::reset() {
|
||||||
|
application = nullptr;
|
||||||
http_parser = nullptr;
|
http_parser = nullptr;
|
||||||
session = nullptr;
|
session = nullptr;
|
||||||
current_middleware_index = 0;
|
current_middleware_index = 0;
|
||||||
|
@ -13,11 +13,14 @@ using namespace brynet;
|
|||||||
using namespace brynet::net;
|
using namespace brynet::net;
|
||||||
using namespace brynet::net::http;
|
using namespace brynet::net::http;
|
||||||
|
|
||||||
|
class Application;
|
||||||
|
|
||||||
class Request {
|
class Request {
|
||||||
public:
|
public:
|
||||||
HTTPParser::Ptr http_parser;
|
HTTPParser::Ptr http_parser;
|
||||||
HttpSession::Ptr session;
|
HttpSession::Ptr session;
|
||||||
HttpResponse *response;
|
HttpResponse *response;
|
||||||
|
Application *application;
|
||||||
|
|
||||||
uint32_t current_middleware_index;
|
uint32_t current_middleware_index;
|
||||||
HandlerInstance handler_instance;
|
HandlerInstance handler_instance;
|
||||||
@ -41,6 +44,7 @@ public:
|
|||||||
void next_stage();
|
void next_stage();
|
||||||
void send();
|
void send();
|
||||||
void send_file(const std::string &p_file_path);
|
void send_file(const std::string &p_file_path);
|
||||||
|
void send_error(int error_code);
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
void setup_url_stack();
|
void setup_url_stack();
|
||||||
|
@ -16,7 +16,7 @@ void PagedArticle::index(Request *request) {
|
|||||||
Article *s = pages[r];
|
Article *s = pages[r];
|
||||||
|
|
||||||
if (s == nullptr) {
|
if (s == nullptr) {
|
||||||
Application::get_instance()->send_error(404, request);
|
request->send_error(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ void PagedArticle::index(Request *request) {
|
|||||||
|
|
||||||
if (page == nullptr) {
|
if (page == nullptr) {
|
||||||
//bad url
|
//bad url
|
||||||
Application::get_instance()->send_error(404, request);
|
request->send_error(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user