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" #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;

View File

@ -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

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); })); 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) {

View File

@ -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);

View File

@ -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() {
} }

View File

@ -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();
}; };

View File

@ -3,6 +3,10 @@
#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;
} }
@ -76,19 +80,31 @@ void Node::set_tree(NodeTree *tree) {
} }
} }
void Node::notification(int 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); _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;
} }

View File

@ -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;