2022-06-25 01:55:54 +02:00
# ifndef WEB_NODE_H
# define WEB_NODE_H
2023-12-18 00:25:33 +01:00
/*************************************************************************/
/* web_node.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2022-07-06 23:03:44 +02:00
# include "../../modules_enabled.gen.h"
2022-08-17 12:53:49 +02:00
# include "core/containers/hash_map.h"
2022-08-17 13:45:14 +02:00
# include "core/object/reference.h"
2023-12-18 00:25:33 +01:00
# include "core/os/rw_lock.h"
2022-08-17 13:17:12 +02:00
# include "core/variant/variant.h"
2022-06-27 01:03:01 +02:00
# include "scene/main/node.h"
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
class WebServerRequest ;
2022-06-25 01:55:54 +02:00
class WebServer ;
class WebPermission ;
2022-07-06 23:03:44 +02:00
# ifdef MODULE_DATABASE_ENABLED
class Database ;
2022-06-25 01:55:54 +02:00
class TableBuilder ;
class QueryBuilder ;
2022-07-23 14:46:05 +02:00
class DatabaseConnection ;
2022-06-25 01:55:54 +02:00
# endif
2022-12-22 19:51:25 +01:00
//note add an alternative queue_delete() -> (with different name) -> if called node tells parent to stop getting routed to, and when rwlock can be locked gets queue_deleted.
2022-06-27 16:37:30 +02:00
//Also add an rwlock just around the handler map (if it's not done already), and add api to lock and unlock it
2022-06-26 21:34:29 +02:00
2022-06-25 01:55:54 +02:00
class WebNode : public Node {
2022-06-27 01:03:01 +02:00
GDCLASS ( WebNode , Node ) ;
2022-06-25 01:55:54 +02:00
public :
2022-06-26 21:34:29 +02:00
enum {
2022-07-07 21:44:41 +02:00
NOTIFICATION_WEB_NODE_WRITE_LOCKED = 2100 ,
2022-06-26 21:34:29 +02:00
} ;
2022-06-25 01:55:54 +02:00
String get_uri_segment ( ) ;
void set_uri_segment ( const String & val ) ;
2022-06-27 01:03:01 +02:00
String get_full_uri ( const bool slash_at_the_end = true ) ;
String get_full_uri_parent ( const bool slash_at_the_end = true ) ;
2022-06-25 01:55:54 +02:00
Ref < WebPermission > get_web_permission ( ) ;
void set_web_permission ( const Ref < WebPermission > & wp ) ;
2022-06-27 01:03:01 +02:00
bool get_routing_enabled ( ) ;
void set_routing_enabled ( const bool value ) ;
2022-06-25 01:55:54 +02:00
2022-07-06 23:03:44 +02:00
# ifdef MODULE_DATABASE_ENABLED
2022-07-23 14:46:05 +02:00
String get_database_table_name ( ) ;
void set_database_table_name ( const String & val ) ;
2022-07-06 23:03:44 +02:00
Ref < Database > get_database ( ) ;
void set_database ( const Ref < Database > & db ) ;
2022-07-23 14:46:05 +02:00
Ref < DatabaseConnection > get_database_connection ( ) ;
2022-06-25 01:55:54 +02:00
Ref < TableBuilder > get_table_builder ( ) ;
Ref < QueryBuilder > get_query_builder ( ) ;
2022-07-23 14:46:05 +02:00
bool get_migrations_enabled ( ) ;
void set_migrations_enabled ( const bool p_enabled ) ;
2022-06-25 01:55:54 +02:00
# endif
2022-06-27 01:03:01 +02:00
void handle_request_main ( Ref < WebServerRequest > request ) ;
virtual void _handle_request_main ( Ref < WebServerRequest > request ) ;
void handle_request ( Ref < WebServerRequest > request ) ;
virtual void _handle_request ( Ref < WebServerRequest > request ) ;
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
void handle_error_send_request ( Ref < WebServerRequest > request , const int error_code ) ;
virtual void _handle_error_send_request ( Ref < WebServerRequest > request , const int error_code ) ;
2022-06-26 21:34:29 +02:00
2022-06-27 01:03:01 +02:00
void render_index ( Ref < WebServerRequest > request ) ;
void render_preview ( Ref < WebServerRequest > request ) ;
void render_menu ( Ref < WebServerRequest > request ) ;
void render_main_menu ( Ref < WebServerRequest > request ) ;
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
virtual void _render_index ( Ref < WebServerRequest > request ) ;
virtual void _render_preview ( Ref < WebServerRequest > request ) ;
virtual void _render_menu ( Ref < WebServerRequest > request ) ;
virtual void _render_main_menu ( Ref < WebServerRequest > request ) ;
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
void create_table ( ) ;
void drop_table ( ) ;
2023-09-22 16:21:37 +02:00
void update_table ( const int p_current_table_version ) ;
2022-07-23 14:46:05 +02:00
void create_default_entries ( const int p_seed ) ;
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
virtual void _create_table ( ) ;
virtual void _drop_table ( ) ;
2023-09-22 16:21:37 +02:00
virtual void _update_table ( const int p_current_table_version ) ;
2022-07-23 14:46:05 +02:00
virtual void _create_default_entries ( const int p_seed ) ;
2022-06-27 01:03:01 +02:00
2022-07-23 14:46:05 +02:00
void migrate ( const bool p_clear , const bool p_should_seed , const int p_seed ) ;
virtual void _migrate ( const bool p_clear , const bool p_should_seed , const int p_seed ) ;
2022-06-25 01:55:54 +02:00
2022-06-27 01:03:01 +02:00
bool try_route_request_to_children ( Ref < WebServerRequest > request ) ;
WebNode * get_request_handler_child ( Ref < WebServerRequest > request ) ;
2022-06-25 01:55:54 +02:00
void build_handler_map ( ) ;
void clear_handlers ( ) ;
2022-06-27 01:03:01 +02:00
void request_write_lock ( ) ;
2022-06-25 01:55:54 +02:00
WebServer * get_server ( ) ;
2022-06-27 01:03:01 +02:00
WebNode * get_web_root ( ) ;
2022-06-25 01:55:54 +02:00
WebNode * get_parent_webnode ( ) ;
2022-06-27 01:03:01 +02:00
void add_child_notify ( Node * p_child ) ;
void remove_child_notify ( Node * p_child ) ;
2022-06-25 01:55:54 +02:00
WebNode ( ) ;
~ WebNode ( ) ;
protected :
2022-06-27 01:03:01 +02:00
void _notification ( const int what ) ;
static void _bind_methods ( ) ;
2022-06-25 01:55:54 +02:00
String _uri_segment ;
2022-07-06 23:03:44 +02:00
# ifdef MODULE_DATABASE_ENABLED
2022-07-23 14:46:05 +02:00
String _database_table_name ;
2022-07-06 23:03:44 +02:00
Ref < Database > _database ;
2022-07-23 14:46:05 +02:00
int _migrations_enabled ;
2022-06-25 01:55:54 +02:00
# endif
bool _routing_enabled ;
WebNode * _index_node ;
2022-06-27 01:03:01 +02:00
HashMap < String , WebNode * > _node_route_map ;
2022-07-01 18:50:56 +02:00
RWLock _handler_map_lock ;
2022-06-25 01:55:54 +02:00
Ref < WebPermission > _web_permission ;
2022-06-27 01:03:01 +02:00
bool _write_lock_requested ;
RWLock _rw_lock ;
2022-06-25 01:55:54 +02:00
} ;
2022-06-26 21:34:29 +02:00
# endif