Started reworking routing.

This commit is contained in:
Relintai 2022-01-08 10:50:07 +01:00
parent 10d3f2fd87
commit 01a5e1881d
8 changed files with 112 additions and 23 deletions

View File

@ -13,6 +13,13 @@
#include "core/database/table_builder.h"
#endif
String WebNode::get_uri_segment() {
return _uri_segment;
}
void WebNode::set_uri_segment(const String &val) {
_uri_segment = val;
}
Settings *WebNode::get_settings() {
if (_settings) {
return _settings;
@ -53,9 +60,9 @@ Ref<QueryBuilder> WebNode::get_query_builder() {
}
void WebNode::set_database(Database *db) {
_database = db;
_database = db;
// todo send event to children when it's implemented?
// todo send event to children when it's implemented?
}
#endif

View File

@ -17,6 +17,9 @@ class WebNode : public Node {
RCPP_OBJECT(WebNode, Node);
public:
String get_uri_segment();
void set_uri_segment(const String &val);
Settings *get_settings();
void set_settings(Settings *settings);
@ -39,6 +42,8 @@ public:
~WebNode();
protected:
String _uri_segment;
Settings *_settings;
#ifdef DATABASES_ENABLED

View File

@ -30,7 +30,9 @@ 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();
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
@ -39,6 +41,9 @@ void WebRoot::default_routing_middleware(Object *instance, Request *request) {
return;
}
//call parent handle default
//from this this will be handled by web router node by default
HandlerInstance handler_data;
//std::function<void(Object *, Request *)> func;
@ -77,10 +82,23 @@ void WebRoot::default_404_error_handler(int error_code, Request *request) {
}
void WebRoot::handle_request_main(Request *request) {
request->middleware_stack = &middlewares;
//request->middleware_stack = &middlewares;
//note that middlewares handle the routing -> WebRoot::default_routing_middleware by default
request->next_stage();
//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);
return;
}
//normal routing
WebRouterNode::handle_request_main(request);
}
void WebRoot::send_error(int error_code, Request *request) {

View File

@ -14,6 +14,19 @@
class Request;
class WebServer;
//Middleware turn into a class (reference)
//add them to a folder
//method should return bool -> true to continue, false if done
//Error handlers
//Add to WebNode api -> send_error(code) v handle_error_send_request(request, int error, variant additional data ?);
//have generic one implemented by default
//Webroot overrides -> uses same logic as now
//notification -> variant additional data?
//FileCache -> set up, for this webroot, don't use singleton
class WebRoot : public WebRouterNode {
RCPP_OBJECT(WebRoot, WebRouterNode);

View File

@ -4,6 +4,29 @@
void WebRouterNode::handle_request_main(Request *request) {
}
void WebRouterNode::build_handler_map() {
}
void WebRouterNode::_notification(const int what) {
switch (what) {
case NOTIFICATION_ENTER_TREE:
build_handler_map();
break;
case NOTIFICATION_CHILD_ADDED:
if (is_in_tree()) {
build_handler_map();
}
break;
case NOTIFICATION_CHILD_REMOVED:
if (is_in_tree()) {
build_handler_map();
}
break;
default:
break;
}
}
WebRouterNode::WebRouterNode() :
WebNode() {
}

View File

@ -9,6 +9,10 @@ class WebRouterNode : public WebNode {
public:
void handle_request_main(Request *request);
void build_handler_map();
void _notification(const int what);
WebRouterNode();
~WebRouterNode();
};

View File

@ -3,23 +3,27 @@
#include "node_tree.h"
bool Node::is_in_tree() const {
return _in_tree;
}
Node *Node::get_parent() {
return _parent;
}
void Node::set_parent(Node *parent) {
if (_parent == parent) {
return;
}
if (_parent == parent) {
return;
}
if (_parent) {
notification(NOTIFICATION_UNPARENTED);
}
if (_parent) {
notification(NOTIFICATION_UNPARENTED);
}
_parent = parent;
if (_parent) {
notification(NOTIFICATION_PARENTED);
}
if (_parent) {
notification(NOTIFICATION_PARENTED);
}
}
int Node::get_child_count() {
@ -35,15 +39,15 @@ void Node::add_child(Node *child) {
_children.push_back(child);
child->set_parent(this);
notification(NOTIFICATION_CHILD_ADDED);
notification(NOTIFICATION_CHILD_ADDED);
}
void Node::remove_child_index(int index) {
Node *c = _children[index];
Node *c = _children[index];
_children.remove_keep_order(index);
c->set_parent(nullptr);
notification(NOTIFICATION_CHILD_REMOVED);
notification(NOTIFICATION_CHILD_REMOVED);
}
void Node::remove_child(Node *child) {
ERR_FAIL_COND(!child);
@ -54,7 +58,7 @@ void Node::remove_child(Node *child) {
if (c == child) {
_children.remove_keep_order(i);
child->set_parent(nullptr);
notification(NOTIFICATION_CHILD_REMOVED);
notification(NOTIFICATION_CHILD_REMOVED);
return;
}
}
@ -76,19 +80,31 @@ void Node::set_tree(NodeTree *tree) {
}
}
void Node::notification(int what) {
_notification(what);
void Node::notification(const int what) {
switch (what) {
case NOTIFICATION_EXIT_TREE:
_in_tree = false;
break;
case NOTIFICATION_ENTER_TREE:
_in_tree = true;
break;
default:
break;
}
_notification(what);
for (int i = 0; i < _children.size(); ++i) {
_children[i]->notification(what);
}
}
void Node::_notification(int what) {
void Node::_notification(const int what) {
}
Node::Node() :
Object() {
_in_tree = false;
_parent = nullptr;
_tree = nullptr;
}

View File

@ -20,6 +20,8 @@ public:
NOTIFICATION_CHILD_MOVED = 6,
};
bool is_in_tree() const;
Node *get_parent();
void set_parent(Node *parent);
@ -32,13 +34,14 @@ public:
NodeTree *get_tree();
void set_tree(NodeTree *tree);
virtual void notification(int what);
virtual void _notification(int what);
virtual void notification(const int what);
virtual void _notification(const int what);
Node();
~Node();
protected:
bool _in_tree;
Node * _parent;
Vector<Node *> _children;
NodeTree *_tree;