Moved routing to a middleware, so it's easily customizable, and also it is now able to do async things.

This commit is contained in:
Relintai 2020-11-26 12:10:21 +01:00
parent 40471f0525
commit c3922e17d2
3 changed files with 20 additions and 13 deletions

View File

@ -19,20 +19,10 @@ void Application::setup_routes() {
} }
void Application::setup_middleware() { void Application::setup_middleware() {
middlewares.push_back(Application::default_routing_middleware);
} }
void Application::default_fallback_error_handler(int error_code, Request *request) { void Application::default_routing_middleware(Request *request) {
request->response->setBody(default_generic_error_body);
request->send();
}
void Application::default_404_error_handler(int error_code, Request *request) {
request->response->setBody(default_error_404_body);
request->send();
}
void Application::handle_request(Request *request) {
std::string path = request->http_parser->getPath(); std::string path = request->http_parser->getPath();
if (FileCache::get_singleton()->wwwroot_has_file(path)) { if (FileCache::get_singleton()->wwwroot_has_file(path)) {
@ -68,8 +58,23 @@ void Application::handle_request(Request *request) {
} }
request->handler_func = func; request->handler_func = func;
request->next_stage();
}
void Application::default_fallback_error_handler(int error_code, Request *request) {
request->response->setBody(default_generic_error_body);
request->send();
}
void Application::default_404_error_handler(int error_code, Request *request) {
request->response->setBody(default_error_404_body);
request->send();
}
void Application::handle_request(Request *request) {
request->middleware_stack = &middlewares; request->middleware_stack = &middlewares;
//note that middlewares handle the routing -> Application::default_routing_middleware by default
request->next_stage(); request->next_stage();
} }

View File

@ -23,6 +23,8 @@ public:
virtual void setup_routes(); virtual void setup_routes();
virtual void setup_middleware(); virtual void setup_middleware();
static void default_routing_middleware(Request *request);
Application(); Application();
virtual ~Application(); virtual ~Application();

View File

@ -43,7 +43,7 @@ void RDNApplication::setup_routes() {
void RDNApplication::setup_middleware() { void RDNApplication::setup_middleware() {
Application::setup_middleware(); Application::setup_middleware();
middlewares.push_back(RDNApplication::session_middleware_func); //middlewares.push_back(RDNApplication::session_middleware_func);
} }
RDNApplication::RDNApplication() : Application() { RDNApplication::RDNApplication() : Application() {