mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
Implemented the new node based routing.
This commit is contained in:
parent
01a5e1881d
commit
e0b76706d9
@ -30,7 +30,6 @@ void WebRoot::setup_middleware() {
|
||||
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
|
||||
std::string path = request->get_path_full();
|
||||
@ -86,14 +85,8 @@ void WebRoot::handle_request_main(Request *request) {
|
||||
// note that middlewares handle the routing -> WebRoot::default_routing_middleware by default
|
||||
// request->next_stage();
|
||||
|
||||
|
||||
// handle files first
|
||||
//todo make this a method for easier override
|
||||
std::string path = request->get_path_full();
|
||||
|
||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||
send_file(path, request);
|
||||
|
||||
if (try_send_wwwroot_file(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -101,6 +94,18 @@ void WebRoot::handle_request_main(Request *request) {
|
||||
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 true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void WebRoot::send_error(int error_code, Request *request) {
|
||||
std::function<void(int, Request *)> func = error_handler_map[error_code];
|
||||
|
||||
@ -156,7 +161,8 @@ WebServer *WebRoot::get_server() {
|
||||
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