From 6478f546890ff8cb04bf8e6b97cef63777f01427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= <24468819+Yagich@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:48:14 +0300 Subject: [PATCH] add initial server implementation --- Main.gd | 81 +++++++++++++++++++++++++++++++ Main.tscn | 8 +++ addons/http_server/http_server.gd | 4 +- project.godot | 1 + 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 Main.gd create mode 100644 Main.tscn diff --git a/Main.gd b/Main.gd new file mode 100644 index 0000000..efb839b --- /dev/null +++ b/Main.gd @@ -0,0 +1,81 @@ +extends Control + +const mime_types: Dictionary = { + "html": "text/html", + "htm": "text/html", + "md": "text/plain", + "css": "text/css", + "txt": "text/plain", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + } + +var _server: HTTPServer = null + +var files: Array = [] + + +func _ready() -> void: + _start_server() + + +func _start_server(port: int = 3001) -> void: + if _server: + return + + _server = HTTPServer.new() + var dir := Directory.new() + if dir.open("res://server_files") == OK: + dir.list_dir_begin() + var file_name := dir.get_next() + while file_name != "": + if !dir.current_is_dir(): + if file_name.get_extension() == "import": + file_name = dir.get_next() + continue + + print(file_name) + + _server.endpoint(HTTPServer.Method.GET, "/%s" % file_name, funcref(self, "_serve_file"), [file_name]) + + file_name = dir.get_next() + + _server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"), ["index.html"]) + + + _server.listen(port) + + +func _stop_server() -> void: + if _server: + _server = null + + +func _process(_delta: float) -> void: + if _server == null: + return + + _server.take_connection() + + +func _serve_file(request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void: + var file_name: String = binds[0] as String + print(file_name) + + var f = File.new() + f.open("res://server_files/%s" % file_name, File.READ) + + var mime = get_mime_type(file_name) + + response.header("content-type", get_mime_type(file_name)) + + response.data(f.get_as_text()) +# else: +# response.header("content-type", "text/plain") +# response.data("500 - Read Error") + + +func get_mime_type(file_name: String) -> String: + var ext := file_name.get_extension().to_lower() + return mime_types[ext] if ext in mime_types else "application/octet-stream" diff --git a/Main.tscn b/Main.tscn new file mode 100644 index 0000000..ad497e0 --- /dev/null +++ b/Main.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://Main.gd" type="Script" id=1] + +[node name="Main" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) diff --git a/addons/http_server/http_server.gd b/addons/http_server/http_server.gd index ad527a2..e22ee8a 100644 --- a/addons/http_server/http_server.gd +++ b/addons/http_server/http_server.gd @@ -13,7 +13,7 @@ const Status = preload("res://addons/http_server/status.gd") var __endpoints: Dictionary = { # key: [Int, String], array with 0 index representing method, 1 index representing endpoint - # value: [FuncRef, Variant], index 0 = reference to function to call, index 1 = binds to pass to func + # value: [FuncRef, Array], index 0 = reference to function to call, index 1 = binds to pass to func } var __fallback: FuncRef = null var __server: TCP_Server = null @@ -21,7 +21,7 @@ var __server: TCP_Server = null # Public methods -func endpoint(type: int, endpoint: String, function: FuncRef, binds = null) -> void: +func endpoint(type: int, endpoint: String, function: FuncRef, binds: Array = []) -> void: var endpoint_hash: Array = [type, endpoint] if endpoint_hash in __endpoints: print( diff --git a/project.godot b/project.godot index 619b742..c413bbf 100644 --- a/project.godot +++ b/project.godot @@ -21,6 +21,7 @@ _global_script_class_icons={ [application] config/name="Ticle Frontend" +run/main_scene="res://Main.tscn" config/icon="res://icon.png" [editor_plugins]