From b2832cb3a82d3cfc75c90bbe2aaade8c45bc26ae Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 14 Jun 2023 17:10:29 +0200 Subject: [PATCH] Added a new demo showing an sqlite web + user management based setup. --- web/users_sqlite/HTTPSessionManagerDB.gd | 10 +++ web/users_sqlite/Main.gd | 35 +++++++++++ web/users_sqlite/Main.tscn | 74 +++++++++++++++++++++++ web/users_sqlite/WebRoot.gd | 18 ++++++ web/users_sqlite/WebServerSimple.gd | 16 +++++ web/users_sqlite/default_env.tres | 7 +++ web/users_sqlite/icon.png | Bin 0 -> 3218 bytes web/users_sqlite/icon.png.import | 35 +++++++++++ web/users_sqlite/project.pandemonium | 25 ++++++++ 9 files changed, 220 insertions(+) create mode 100644 web/users_sqlite/HTTPSessionManagerDB.gd create mode 100644 web/users_sqlite/Main.gd create mode 100644 web/users_sqlite/Main.tscn create mode 100644 web/users_sqlite/WebRoot.gd create mode 100644 web/users_sqlite/WebServerSimple.gd create mode 100644 web/users_sqlite/default_env.tres create mode 100644 web/users_sqlite/icon.png create mode 100644 web/users_sqlite/icon.png.import create mode 100644 web/users_sqlite/project.pandemonium diff --git a/web/users_sqlite/HTTPSessionManagerDB.gd b/web/users_sqlite/HTTPSessionManagerDB.gd new file mode 100644 index 0000000..4c446ba --- /dev/null +++ b/web/users_sqlite/HTTPSessionManagerDB.gd @@ -0,0 +1,10 @@ +extends HTTPSessionManagerDB + +func _ready() -> void: + DatabaseManager.connect("initialized", self, "on_databases_initialized", [], CONNECT_ONESHOT) + # You could also connect to the migration signal, and write migrations if you need them + +func on_databases_initialized() -> void: + # Load sessions after the databases are initialized + # This happens on the Main node. + call_deferred("load_sessions") diff --git a/web/users_sqlite/Main.gd b/web/users_sqlite/Main.gd new file mode 100644 index 0000000..68088ed --- /dev/null +++ b/web/users_sqlite/Main.gd @@ -0,0 +1,35 @@ + +extends Node + +export(String) var database_location : String = "user://database.sqlite" + +func _ready() -> void: + var d : Directory = Directory.new() + var bd : String = database_location.get_base_dir() + var loc : String = d.get_filesystem_abspath_for(bd).append_path(database_location.get_file()) + + var file : File = File.new() + if !file.file_exists(loc): + PLogger.log_message("Database file doesn't exists, will run migrations!") + call_deferred("migrate") + else: + call_deferred("db_initialized") + + var db : SQLite3Database = SQLite3Database.new() + db.connection_string = loc + DatabaseManager.add_database(db) + + +func migrate() -> void: + PLogger.log_message("Running migrations!") + DatabaseManager.connect("migration", self, "_migration") + DatabaseManager.migrate(true, false, 0) + + call_deferred("db_initialized") + +func db_initialized() -> void: + DatabaseManager.initialized() + +func _migration(clear: bool, should_seed: bool, pseed: int) -> void: + #create admin account + pass diff --git a/web/users_sqlite/Main.tscn b/web/users_sqlite/Main.tscn new file mode 100644 index 0000000..5fdfd76 --- /dev/null +++ b/web/users_sqlite/Main.tscn @@ -0,0 +1,74 @@ +[gd_scene load_steps=8 format=2] + +[ext_resource path="res://WebServerSimple.gd" type="Script" id=1] +[ext_resource path="res://WebRoot.gd" type="Script" id=2] +[ext_resource path="res://HTTPSessionManagerDB.gd" type="Script" id=3] +[ext_resource path="res://Main.gd" type="Script" id=4] + +[sub_resource type="SessionSetupWebServerMiddleware" id=3] + +[sub_resource type="UserSessionSetupWebServerMiddleware" id=4] + +[sub_resource type="CSRFTokenWebServerMiddleware" id=5] +ignored_urls = PoolStringArray( "/user/login", "/user/register" ) + +[node name="Main" type="Node"] +script = ExtResource( 4 ) + +[node name="UserManagerDB" type="UserManagerDB" parent="."] + +[node name="WebServerSimple" type="WebServerSimple" parent="."] +script = ExtResource( 1 ) + +[node name="WebRoot" type="WebRoot" parent="WebServerSimple"] +www_root_path = "res://www/" +middlewares = [ SubResource( 3 ), SubResource( 4 ), SubResource( 5 ) ] +script = ExtResource( 2 ) +menu_str = "
+index
+login
+register
+settings
+logout
+
+ +" + +[node name="StaticWebPage" type="StaticWebPage" parent="WebServerSimple/WebRoot"] +uri_segment = "/" +data = " +You can go and log in on the users page here: Login
+
+Note that in this demo sessions and users are saved in an SQLite database here: \"user://database.sqlite\"
+
+There are no users by default.
+
+" + +[node name="UserWebPage" type="UserWebPage" parent="WebServerSimple/WebRoot"] +uri_segment = "user" +logged_out_render_type = 1 +logged_out_redirect_url = "/user/login" + +[node name="UserLoginWebPage" type="UserLoginWebPage" parent="WebServerSimple/WebRoot/UserWebPage"] +uri_segment = "login" +logged_in_render_type = 1 +logged_in_redirect_url = "/" + +[node name="UserRegisterWebPage" type="UserRegisterWebPage" parent="WebServerSimple/WebRoot/UserWebPage"] +uri_segment = "register" +logged_in_render_type = 1 +logged_in_redirect_url = "/" + +[node name="UserLogoutWebPage" type="UserLogoutWebPage" parent="WebServerSimple/WebRoot/UserWebPage"] +uri_segment = "logout" +logged_out_render_type = 1 +logged_out_redirect_url = "/" + +[node name="UserSettingsWebPage" type="UserSettingsWebPage" parent="WebServerSimple/WebRoot/UserWebPage"] +uri_segment = "settings" +logged_out_render_type = 1 +logged_out_redirect_url = "/user/login" + +[node name="HTTPSessionManagerDB" type="HTTPSessionManagerDB" parent="WebServerSimple"] +script = ExtResource( 3 ) diff --git a/web/users_sqlite/WebRoot.gd b/web/users_sqlite/WebRoot.gd new file mode 100644 index 0000000..8697445 --- /dev/null +++ b/web/users_sqlite/WebRoot.gd @@ -0,0 +1,18 @@ +extends WebRoot + +export(String, MULTILINE) var menu_str : String + +func _render_main_menu(request: WebServerRequest) -> void: + # You can render the menu differently for logged in users for example + # The middlewares will run before routing (in order they are in the middlewares property) + request.body += menu_str + + # The UserSessionSetupWebServerMiddleware makes this available here: + # If you want to do this manually, you can do it via request.session + the UserDB singleton + # I recommend looking at the middleware code on the engine c++ side to see an example + var user : User = request.get_meta("user") + + if user: + request.body += "You are logged in as : " + user.user_name + ".

" + else: + request.body += "You are not logged in.

" diff --git a/web/users_sqlite/WebServerSimple.gd b/web/users_sqlite/WebServerSimple.gd new file mode 100644 index 0000000..9c7d7e9 --- /dev/null +++ b/web/users_sqlite/WebServerSimple.gd @@ -0,0 +1,16 @@ +extends WebServerSimple + + +# Declare member variables here. Examples: +# var a: int = 2 +# var b: String = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + start() + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta: float) -> void: +# pass diff --git a/web/users_sqlite/default_env.tres b/web/users_sqlite/default_env.tres new file mode 100644 index 0000000..4f08e8f --- /dev/null +++ b/web/users_sqlite/default_env.tres @@ -0,0 +1,7 @@ +[gd_resource type="Environment3D" load_steps=2 format=2] + +[sub_resource type="ProceduralSky" id=1] + +[resource] +background_mode = 2 +background_sky = SubResource( 1 ) diff --git a/web/users_sqlite/icon.png b/web/users_sqlite/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..87f1f7549e0489d0758939152cd1f879f1e653b5 GIT binary patch literal 3218 zcmV;D3~lp?P)Px>Oi4sRRCt`-n|p9v)qTf5=iI$}?`o~pW3{W5wUUr5+ZZ>+Ha2kvrbE(n3@r>e z6xz@>Qyxj3Ng!!SGDBuM`Ku++LLq@nT^K`)n@5w-mPttloU|q-HU{G-mStPWl5IU# zZ>{!q?>+rvB_U}a+Lc#+^!wM&{r&Fm{Lb&5^EM0?_ip!zwaziOKi4c#X)IUXr zhJa}U`2Bta{8R|>nair-C4y4wCkpNv8X7`WRaGIS{Gt@%dkYzLUB_dZNKLyOdR{af z+a?x|lS-vBX-e&I9A~GfsHkYxU9O{ZmwkD8Id!2BE|=?)D!g2>F5+YG_y^U)9vBc;47$}R__lvMb9BoYY{$s_2=)Z9m#TtcZ$i1cA#8b(Q$y=7Sp4G&}6HZ^`fZnt|$as~zmxzN)? zGMQv`Yb(`NRTrg~%jLo}jM-Iy5Sv^G5tw0Es-knDl%oB_2{tJW7lQu2e#RRc(#LEO z=sbIt=8*|jYi^XG^KM5cm76LqdTYAf?vD%rb@QYNAu``660ul}H8yyphAt&xmr3wk zFK4^D3VPph9NJHvV$E2TN|%lh0x2YGHJxZQdeQnVoMXv7T`HBrBjz8QKF!72(Misn zIkV{d7kYa5SS*EC&X?iUG-C01Np*$gF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boX}N z{`ddPN^e}Zj!*v4J^XU_4|95(U}FPsz4|IoKe!_+J!M-cf8iNuQCnatPeCL$os{UN zK_W+jvS#xQwEgBcJiFsdS?RSc&Fnn*4sO#R6;EX4@rT14eQR${JuFpR53wjGnAEkL zwZR#Mx7@l7e?u65Ll~*+>5i_J9_C{=-NcFg@8*<0($^0_Wgs})4VEq!-pWe+4Pk;! zP1Lnq!$|)?P8l&}NgPz+;*?cJ|JHDv@M-c$lv|_iSDn9fxy=ugU2vFfSZE zm{-Qh1G$aZWhZIQ(+60Hq72~k`RE>u(5z=#SfKc`mbJL1Gd#CxvZ6YDudlYDk?Oj1 z0&{TBp4T}fQEyqDQmu71?N)fS=^jk?13t|%?1Pw|Se5tZ1CpbAahIfw5 z9HkhuZBC`)#61Sli3!%YJ=DA0xP@G_eO(M}$032D-%iqQr3ls5qG=kP(I{o(lU!?< zxTmk^*12>#$HoeZ2MRWTQi|l{B*HAmavaWENiLW=HK8CaRaNM^PAZimGBm{dBO^>s zPU5m0f-Vjz#ODNBNu?NbY=$ihTi00O_2T!}v&HY9*Y=9X<9yKH zj~N+emCL{-BxB>_ghHX>46xV&o^TxYB%=g_L283R)*Hq=x~}Wgg+kO#4;)VmA$ z-=)^;lA*u9e_pXhDD{r;`FyQzT|X>@n7wCK;qws+g$nAgUk$Kro8G=Y#>dBJpDCrH z$y92CsH&_)lkyQEWkwojxZRi@&$5=dmJ`Zxh{xk||06UDN2xE{w*CCf%!J?Xe@zN; z`=wR!@<2JxYmwpMy8ty4)hLv@Lpjb@l~RRW1Gx&Jl$uhG^A+GurceEOYrCeR0vu;M1& literal 0 HcmV?d00001 diff --git a/web/users_sqlite/icon.png.import b/web/users_sqlite/icon.png.import new file mode 100644 index 0000000..a4c02e6 --- /dev/null +++ b/web/users_sqlite/icon.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.png" +dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=true +svg/scale=1.0 diff --git a/web/users_sqlite/project.pandemonium b/web/users_sqlite/project.pandemonium new file mode 100644 index 0000000..6ae35c5 --- /dev/null +++ b/web/users_sqlite/project.pandemonium @@ -0,0 +1,25 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=4 + +[application] + +config/name="Users Static" +run/main_scene="res://Main.tscn" +config/icon="res://icon.png" + +[physics] + +common/enable_pause_aware_picking=true + +[rendering] + +vram_compression/import_etc=true +vram_compression/import_etc2=false +environment/default_environment="res://default_env.tres"