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:
Relintai 2021-04-28 22:50:05 +02:00
parent c5ea34afdc
commit 614a496b7d
7 changed files with 19 additions and 15 deletions

View File

@ -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<std::string, HandlerInstance> Application::main_route_map;
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::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_generic_error_body = "<html><body>Internal server error! :(</body></html>";

View File

@ -40,8 +40,6 @@ public:
Application();
virtual ~Application();
static Application *get_instance();
public:
static HandlerInstance index_func;
static std::map<std::string, HandlerInstance> main_route_map;
@ -53,9 +51,6 @@ public:
protected:
static std::mutex _update_registered_requests_mutex;
static std::vector<Request *> _update_registered_requests;
private:
static Application *_instance;
};
#endif

View File

@ -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>(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() {

View File

@ -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<HttpSession *, Request *> _request_map;
std::mutex _request_map_mutex;

View File

@ -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;

View File

@ -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();

View File

@ -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;
}