Cleaned up WebRoot.

This commit is contained in:
Relintai 2022-01-08 12:35:44 +01:00
parent 15b9bb17e5
commit dff3156138
2 changed files with 9 additions and 24 deletions

View File

@ -12,12 +12,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
void WebRoot::load_settings() { void WebRoot::setup() {
setup_error_handlers();
setup_middleware();
} }
void WebRoot::setup_routes() { void WebRoot::setup_error_handlers() {
default_error_handler_func = WebRoot::default_fallback_error_handler; default_error_handler_func = WebRoot::default_fallback_error_handler;
error_handler_map[404] = WebRoot::default_404_error_handler; error_handler_map[404] = WebRoot::default_404_error_handler;
} }
@ -90,19 +91,12 @@ bool WebRoot::try_send_wwwroot_file(Request *request) {
return false; return false;
} }
void WebRoot::send_error(int error_code, Request *request) {
}
void WebRoot::send_file(const std::string &path, Request *request) { void WebRoot::send_file(const std::string &path, Request *request) {
std::string fp = FileCache::get_singleton()->wwwroot + path; std::string fp = FileCache::get_singleton()->wwwroot + path;
request->send_file(fp); request->send_file(fp);
} }
void WebRoot::migrate() {
}
void WebRoot::register_request_update(Request *request) { void WebRoot::register_request_update(Request *request) {
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex); std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
@ -135,12 +129,13 @@ void WebRoot::update() {
WebRoot::WebRoot() : WebRoot::WebRoot() :
WebRouterNode() { WebRouterNode() {
setup();
} }
WebRoot::~WebRoot() { WebRoot::~WebRoot() {
main_route_map.clear();
error_handler_map.clear(); error_handler_map.clear();
middlewares.clear(); _middlewares.clear();
} }
std::string WebRoot::default_error_404_body = "<html><body>404 :(</body></html>"; std::string WebRoot::default_error_404_body = "<html><body>404 :(</body></html>";

View File

@ -17,13 +17,10 @@ class Request;
// FileCache -> set up, for this webroot, don't use singleton // FileCache -> set up, for this webroot, don't use singleton
// clean up unused things here
// remove handler instances! // remove handler instances!
// Update the rest of the modules to the new systems // Update the rest of the modules to the new systems
// remove middleware stack from request
class WebRoot : public WebRouterNode { class WebRoot : public WebRouterNode {
RCPP_OBJECT(WebRoot, WebRouterNode); RCPP_OBJECT(WebRoot, WebRouterNode);
@ -35,20 +32,17 @@ public:
void handle_error_send_request(Request *request, const int error_code); void handle_error_send_request(Request *request, const int error_code);
bool try_send_wwwroot_file(Request *request); bool try_send_wwwroot_file(Request *request);
void send_error(int error_code, Request *request);
void send_file(const std::string &path, Request *request); void send_file(const std::string &path, Request *request);
static void default_fallback_error_handler(Request *request, int error_code); static void default_fallback_error_handler(Request *request, int error_code);
static void default_404_error_handler(Request *request, int error_code); static void default_404_error_handler(Request *request, int error_code);
virtual void load_settings(); virtual void setup();
virtual void setup_routes(); virtual void setup_error_handlers();
virtual void setup_middleware(); virtual void setup_middleware();
void default_routing_middleware(Object *instance, Request *request); void default_routing_middleware(Object *instance, Request *request);
virtual void migrate();
void register_request_update(Request *request); void register_request_update(Request *request);
void unregister_request_update(Request *request); void unregister_request_update(Request *request);
void update(); void update();
@ -57,10 +51,6 @@ public:
virtual ~WebRoot(); virtual ~WebRoot();
public: public:
HandlerInstance index_func;
std::map<std::string, HandlerInstance> main_route_map;
std::vector<HandlerInstance> middlewares;
Vector<Ref<Middleware> > _middlewares; Vector<Ref<Middleware> > _middlewares;
std::map<int, std::function<void(Request *, int)> > error_handler_map; std::map<int, std::function<void(Request *, int)> > error_handler_map;