mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Implemented the new node based routing.
This commit is contained in:
parent
01a5e1881d
commit
e0b76706d9
@ -24,15 +24,14 @@ void WebRoot::setup_routes() {
|
||||
}
|
||||
|
||||
void WebRoot::setup_middleware() {
|
||||
//If you want sessions add this to your inherited class. Should probably be the first one.
|
||||
//middlewares.push_back(HandlerInstance(::SessionManager::session_setup_middleware));
|
||||
// If you want sessions add this to your inherited class. Should probably be the first one.
|
||||
// middlewares.push_back(HandlerInstance(::SessionManager::session_setup_middleware));
|
||||
|
||||
middlewares.push_back(HandlerInstance([this](Object *instance, Request *request){ this->default_routing_middleware(instance, request); }));
|
||||
middlewares.push_back(HandlerInstance([this](Object *instance, Request *request) { this->default_routing_middleware(instance, request); }));
|
||||
}
|
||||
|
||||
|
||||
void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
||||
//handle default phase 1
|
||||
// handle default phase 1
|
||||
std::string path = request->get_path_full();
|
||||
|
||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||
@ -41,16 +40,16 @@ void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
||||
return;
|
||||
}
|
||||
|
||||
//call parent handle default
|
||||
// call parent handle default
|
||||
|
||||
//from this this will be handled by web router node by default
|
||||
// from this this will be handled by web router node by default
|
||||
HandlerInstance handler_data;
|
||||
|
||||
//std::function<void(Object *, Request *)> func;
|
||||
// std::function<void(Object *, Request *)> func;
|
||||
|
||||
//if (path == "/") {
|
||||
// if (path == "/") {
|
||||
if (request->get_path_segment_count() == 0) {
|
||||
//quick shortcut
|
||||
// quick shortcut
|
||||
handler_data = index_func;
|
||||
} else {
|
||||
const std::string main_route = request->get_current_path_segment();
|
||||
@ -72,7 +71,7 @@ void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
||||
|
||||
void WebRoot::default_fallback_error_handler(int error_code, Request *request) {
|
||||
request->compiled_body = default_generic_error_body;
|
||||
|
||||
|
||||
request->send();
|
||||
}
|
||||
|
||||
@ -82,23 +81,29 @@ void WebRoot::default_404_error_handler(int error_code, Request *request) {
|
||||
}
|
||||
|
||||
void WebRoot::handle_request_main(Request *request) {
|
||||
//request->middleware_stack = &middlewares;
|
||||
//note that middlewares handle the routing -> WebRoot::default_routing_middleware by default
|
||||
//request->next_stage();
|
||||
// request->middleware_stack = &middlewares;
|
||||
// note that middlewares handle the routing -> WebRoot::default_routing_middleware by default
|
||||
// request->next_stage();
|
||||
|
||||
// handle files first
|
||||
if (try_send_wwwroot_file(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//handle files first
|
||||
//todo make this a method for easier override
|
||||
std::string path = request->get_path_full();
|
||||
// normal routing
|
||||
WebRouterNode::handle_request_main(request);
|
||||
}
|
||||
|
||||
bool WebRoot::try_send_wwwroot_file(Request *request) {
|
||||
const String &path = request->get_path_full();
|
||||
|
||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||
send_file(path, request);
|
||||
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
//normal routing
|
||||
WebRouterNode::handle_request_main(request);
|
||||
return false;
|
||||
}
|
||||
|
||||
void WebRoot::send_error(int error_code, Request *request) {
|
||||
@ -152,11 +157,12 @@ void WebRoot::update() {
|
||||
}
|
||||
|
||||
WebServer *WebRoot::get_server() {
|
||||
//todo this shoult probably be cached
|
||||
// todo this shoult probably be cached
|
||||
return Object::cast_to<WebServer>(get_tree());
|
||||
}
|
||||
|
||||
WebRoot::WebRoot() : WebRouterNode() {
|
||||
WebRoot::WebRoot() :
|
||||
WebRouterNode() {
|
||||
}
|
||||
|
||||
WebRoot::~WebRoot() {
|
||||
|
@ -35,6 +35,7 @@ public:
|
||||
static std::string default_generic_error_body;
|
||||
|
||||
void handle_request_main(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);
|
||||
|
||||
|
@ -1,10 +1,70 @@
|
||||
|
||||
#include "web_router_node.h"
|
||||
|
||||
#include "request.h"
|
||||
|
||||
void WebRouterNode::handle_request_main(Request *request) {
|
||||
if (!try_route_request_to_children(request)) {
|
||||
request->send_error(404);
|
||||
}
|
||||
}
|
||||
|
||||
bool WebRouterNode::try_route_request_to_children(Request *request) {
|
||||
WebNode *handler = nullptr;
|
||||
|
||||
// if (path == "/") {
|
||||
if (request->get_path_segment_count() == 0) {
|
||||
// quick shortcut
|
||||
handler = _index_node;
|
||||
} else {
|
||||
const String &main_route = request->get_current_path_segment();
|
||||
|
||||
handler = _node_route_map[main_route];
|
||||
|
||||
request->push_path();
|
||||
}
|
||||
|
||||
if (!handler) {
|
||||
return false;
|
||||
}
|
||||
|
||||
handler->handle_request_main(request);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebRouterNode::build_handler_map() {
|
||||
_index_node = nullptr;
|
||||
_node_route_map.clear();
|
||||
|
||||
for (int i = 0; i < get_child_count(); ++i) {
|
||||
WebNode *c = Object::cast_to<WebNode>(get_child(i));
|
||||
|
||||
if (!c) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String uri_segment = c->get_uri_segment();
|
||||
|
||||
if (uri_segment == "") {
|
||||
// ignore
|
||||
continue;
|
||||
} else if (uri_segment == "/") {
|
||||
if (_index_node) {
|
||||
RLOG_ERR("You have multiple root nodes!");
|
||||
continue;
|
||||
}
|
||||
|
||||
_index_node = c;
|
||||
} else {
|
||||
if (_node_route_map[uri_segment]) {
|
||||
RLOG_ERR("You have multiple of the same uri! URI:" + uri_segment);
|
||||
continue;
|
||||
}
|
||||
|
||||
_node_route_map[uri_segment] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WebRouterNode::_notification(const int what) {
|
||||
|
@ -3,11 +3,15 @@
|
||||
|
||||
#include "web_node.h"
|
||||
|
||||
#include <map>
|
||||
#include "core/string.h"
|
||||
|
||||
class WebRouterNode : public WebNode {
|
||||
RCPP_OBJECT(WebRouterNode, WebNode);
|
||||
|
||||
public:
|
||||
void handle_request_main(Request *request);
|
||||
bool try_route_request_to_children(Request *request);
|
||||
|
||||
void build_handler_map();
|
||||
|
||||
@ -15,6 +19,10 @@ public:
|
||||
|
||||
WebRouterNode();
|
||||
~WebRouterNode();
|
||||
|
||||
protected:
|
||||
WebNode *_index_node;
|
||||
std::map<String, WebNode *> _node_route_map;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user