mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
Started reworking routing.
This commit is contained in:
parent
10d3f2fd87
commit
01a5e1881d
@ -13,6 +13,13 @@
|
|||||||
#include "core/database/table_builder.h"
|
#include "core/database/table_builder.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
String WebNode::get_uri_segment() {
|
||||||
|
return _uri_segment;
|
||||||
|
}
|
||||||
|
void WebNode::set_uri_segment(const String &val) {
|
||||||
|
_uri_segment = val;
|
||||||
|
}
|
||||||
|
|
||||||
Settings *WebNode::get_settings() {
|
Settings *WebNode::get_settings() {
|
||||||
if (_settings) {
|
if (_settings) {
|
||||||
return _settings;
|
return _settings;
|
||||||
@ -53,9 +60,9 @@ Ref<QueryBuilder> WebNode::get_query_builder() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WebNode::set_database(Database *db) {
|
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
|
#endif
|
||||||
|
@ -17,6 +17,9 @@ class WebNode : public Node {
|
|||||||
RCPP_OBJECT(WebNode, Node);
|
RCPP_OBJECT(WebNode, Node);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
String get_uri_segment();
|
||||||
|
void set_uri_segment(const String &val);
|
||||||
|
|
||||||
Settings *get_settings();
|
Settings *get_settings();
|
||||||
void set_settings(Settings *settings);
|
void set_settings(Settings *settings);
|
||||||
|
|
||||||
@ -39,6 +42,8 @@ public:
|
|||||||
~WebNode();
|
~WebNode();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
String _uri_segment;
|
||||||
|
|
||||||
Settings *_settings;
|
Settings *_settings;
|
||||||
|
|
||||||
#ifdef DATABASES_ENABLED
|
#ifdef DATABASES_ENABLED
|
||||||
|
@ -30,7 +30,9 @@ void WebRoot::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) {
|
void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
||||||
|
//handle default phase 1
|
||||||
std::string path = request->get_path_full();
|
std::string path = request->get_path_full();
|
||||||
|
|
||||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||||
@ -39,6 +41,9 @@ void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//call parent handle default
|
||||||
|
|
||||||
|
//from this this will be handled by web router node by default
|
||||||
HandlerInstance handler_data;
|
HandlerInstance handler_data;
|
||||||
|
|
||||||
//std::function<void(Object *, Request *)> func;
|
//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) {
|
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
|
//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) {
|
void WebRoot::send_error(int error_code, Request *request) {
|
||||||
|
@ -14,6 +14,19 @@
|
|||||||
class Request;
|
class Request;
|
||||||
class WebServer;
|
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 {
|
class WebRoot : public WebRouterNode {
|
||||||
RCPP_OBJECT(WebRoot, WebRouterNode);
|
RCPP_OBJECT(WebRoot, WebRouterNode);
|
||||||
|
|
||||||
|
@ -4,6 +4,29 @@
|
|||||||
void WebRouterNode::handle_request_main(Request *request) {
|
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() :
|
WebRouterNode::WebRouterNode() :
|
||||||
WebNode() {
|
WebNode() {
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,10 @@ class WebRouterNode : public WebNode {
|
|||||||
public:
|
public:
|
||||||
void handle_request_main(Request *request);
|
void handle_request_main(Request *request);
|
||||||
|
|
||||||
|
void build_handler_map();
|
||||||
|
|
||||||
|
void _notification(const int what);
|
||||||
|
|
||||||
WebRouterNode();
|
WebRouterNode();
|
||||||
~WebRouterNode();
|
~WebRouterNode();
|
||||||
};
|
};
|
||||||
|
@ -3,23 +3,27 @@
|
|||||||
|
|
||||||
#include "node_tree.h"
|
#include "node_tree.h"
|
||||||
|
|
||||||
|
bool Node::is_in_tree() const {
|
||||||
|
return _in_tree;
|
||||||
|
}
|
||||||
|
|
||||||
Node *Node::get_parent() {
|
Node *Node::get_parent() {
|
||||||
return _parent;
|
return _parent;
|
||||||
}
|
}
|
||||||
void Node::set_parent(Node *parent) {
|
void Node::set_parent(Node *parent) {
|
||||||
if (_parent == parent) {
|
if (_parent == parent) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_parent) {
|
if (_parent) {
|
||||||
notification(NOTIFICATION_UNPARENTED);
|
notification(NOTIFICATION_UNPARENTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
_parent = parent;
|
_parent = parent;
|
||||||
|
|
||||||
if (_parent) {
|
if (_parent) {
|
||||||
notification(NOTIFICATION_PARENTED);
|
notification(NOTIFICATION_PARENTED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Node::get_child_count() {
|
int Node::get_child_count() {
|
||||||
@ -35,15 +39,15 @@ void Node::add_child(Node *child) {
|
|||||||
_children.push_back(child);
|
_children.push_back(child);
|
||||||
child->set_parent(this);
|
child->set_parent(this);
|
||||||
|
|
||||||
notification(NOTIFICATION_CHILD_ADDED);
|
notification(NOTIFICATION_CHILD_ADDED);
|
||||||
}
|
}
|
||||||
void Node::remove_child_index(int index) {
|
void Node::remove_child_index(int index) {
|
||||||
Node *c = _children[index];
|
Node *c = _children[index];
|
||||||
|
|
||||||
_children.remove_keep_order(index);
|
_children.remove_keep_order(index);
|
||||||
c->set_parent(nullptr);
|
c->set_parent(nullptr);
|
||||||
|
|
||||||
notification(NOTIFICATION_CHILD_REMOVED);
|
notification(NOTIFICATION_CHILD_REMOVED);
|
||||||
}
|
}
|
||||||
void Node::remove_child(Node *child) {
|
void Node::remove_child(Node *child) {
|
||||||
ERR_FAIL_COND(!child);
|
ERR_FAIL_COND(!child);
|
||||||
@ -54,7 +58,7 @@ void Node::remove_child(Node *child) {
|
|||||||
if (c == child) {
|
if (c == child) {
|
||||||
_children.remove_keep_order(i);
|
_children.remove_keep_order(i);
|
||||||
child->set_parent(nullptr);
|
child->set_parent(nullptr);
|
||||||
notification(NOTIFICATION_CHILD_REMOVED);
|
notification(NOTIFICATION_CHILD_REMOVED);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,19 +80,31 @@ void Node::set_tree(NodeTree *tree) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::notification(int what) {
|
void Node::notification(const int what) {
|
||||||
_notification(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) {
|
for (int i = 0; i < _children.size(); ++i) {
|
||||||
_children[i]->notification(what);
|
_children[i]->notification(what);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Node::_notification(int what) {
|
void Node::_notification(const int what) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Node::Node() :
|
Node::Node() :
|
||||||
Object() {
|
Object() {
|
||||||
|
|
||||||
|
_in_tree = false;
|
||||||
_parent = nullptr;
|
_parent = nullptr;
|
||||||
_tree = nullptr;
|
_tree = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ public:
|
|||||||
NOTIFICATION_CHILD_MOVED = 6,
|
NOTIFICATION_CHILD_MOVED = 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool is_in_tree() const;
|
||||||
|
|
||||||
Node *get_parent();
|
Node *get_parent();
|
||||||
void set_parent(Node *parent);
|
void set_parent(Node *parent);
|
||||||
|
|
||||||
@ -32,13 +34,14 @@ public:
|
|||||||
NodeTree *get_tree();
|
NodeTree *get_tree();
|
||||||
void set_tree(NodeTree *tree);
|
void set_tree(NodeTree *tree);
|
||||||
|
|
||||||
virtual void notification(int what);
|
virtual void notification(const int what);
|
||||||
virtual void _notification(int what);
|
virtual void _notification(const int what);
|
||||||
|
|
||||||
Node();
|
Node();
|
||||||
~Node();
|
~Node();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool _in_tree;
|
||||||
Node * _parent;
|
Node * _parent;
|
||||||
Vector<Node *> _children;
|
Vector<Node *> _children;
|
||||||
NodeTree *_tree;
|
NodeTree *_tree;
|
||||||
|
Loading…
Reference in New Issue
Block a user