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() {
middlewares.push_back(Application::default_routing_middleware);
}
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) {
void Application::default_routing_middleware(Request *request) {
std::string path = request->http_parser->getPath();
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
@ -68,8 +58,23 @@ void Application::handle_request(Request *request) {
}
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;
//note that middlewares handle the routing -> Application::default_routing_middleware by default
request->next_stage();
}

View File

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

View File

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