Compare commits

...

6 Commits

10 changed files with 77 additions and 101 deletions

3
.gitignore vendored
View File

@ -9,3 +9,6 @@ export_presets.cfg
# Mono-specific ignores
.mono/
data_*/
# VSCode config folder
.vscode/

35
Main.gd
View File

@ -17,26 +17,7 @@ func _start_server(port: int = 3001) -> void:
return
_server = HTTPServer.new()
var dir := Directory.new()
if dir.open(server_dir) == OK:
if dir.list_dir_begin() != OK:
# TODO: show error to user here
return
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.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"))
if _server.listen(port) != OK:
# TODO: show error to user here
@ -62,13 +43,14 @@ func _process(_delta: float) -> void:
return
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
var file_name: String = binds[0] as String
func _serve_file(request: HTTPServer.Request, response: HTTPServer.Response) -> void:
var file_name: String = request.endpoint()
if file_name == "/": # if the request is for root, serve index
file_name = "index.html"
var f := File.new()
var success = f.open(server_dir.plus_file(file_name), File.READ)
if success == OK:
if success == OK: # TODO: handle other errors like file not found
var mime := mime_types.get(file_name)
response.type(mime)
@ -77,8 +59,9 @@ func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, bi
response.data(data)
else:
response.header("content-type", "text/plain")
response.data("500 - Read Error")
response.type(mime_types.get("txt"))
response.status(500)
response.data("Internal Server Error")
func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void:

View File

@ -20,75 +20,75 @@ __meta__ = {
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
margin_right = 1182.0
margin_bottom = 39.0
margin_bottom = 42.0
alignment = 1
[node name="Label" type="Label" parent="VBoxContainer/HBoxContainer"]
margin_left = 97.0
margin_top = 11.0
margin_right = 191.0
margin_bottom = 28.0
margin_left = 95.0
margin_top = 12.0
margin_right = 189.0
margin_bottom = 29.0
text = "Server folder:"
[node name="ServerPathLabel" type="Label" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 195.0
margin_top = 11.0
margin_right = 448.0
margin_bottom = 28.0
margin_left = 193.0
margin_top = 12.0
margin_right = 446.0
margin_bottom = 29.0
text = "/home/username/long/path/to/server"
[node name="OpenServerFolderButton" type="Button" parent="VBoxContainer/HBoxContainer"]
margin_left = 452.0
margin_right = 520.0
margin_bottom = 39.0
margin_left = 450.0
margin_right = 517.0
margin_bottom = 42.0
rect_min_size = Vector2( 0, 32 )
text = "Open..."
[node name="Label2" type="Label" parent="VBoxContainer/HBoxContainer"]
margin_left = 524.0
margin_top = 11.0
margin_right = 606.0
margin_bottom = 28.0
margin_left = 521.0
margin_top = 12.0
margin_right = 603.0
margin_bottom = 29.0
text = "Server port:"
[node name="PortSpinBox" type="SpinBox" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 610.0
margin_right = 690.0
margin_bottom = 39.0
margin_left = 607.0
margin_right = 687.0
margin_bottom = 42.0
min_value = 81.0
max_value = 8000.0
value = 3001.0
[node name="StartServerButton" type="Button" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 694.0
margin_right = 791.0
margin_bottom = 39.0
margin_left = 691.0
margin_right = 792.0
margin_bottom = 42.0
rect_min_size = Vector2( 0, 32 )
text = "Start server"
[node name="ServerStatusLabel" type="Label" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 795.0
margin_top = 11.0
margin_right = 946.0
margin_bottom = 28.0
margin_left = 796.0
margin_top = 12.0
margin_right = 947.0
margin_bottom = 29.0
text = "Server is not running."
[node name="OpenBrowserButton" type="Button" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 950.0
margin_right = 1084.0
margin_bottom = 39.0
margin_left = 951.0
margin_right = 1086.0
margin_bottom = 42.0
rect_min_size = Vector2( 0, 32 )
size_flags_horizontal = 12
disabled = true
text = "Open in browser"
[node name="HSplitContainer" type="HSplitContainer" parent="VBoxContainer"]
margin_top = 48.0
margin_top = 51.0
margin_right = 1182.0
margin_bottom = 611.0
size_flags_vertical = 3
@ -96,17 +96,17 @@ split_offset = -263
[node name="VBoxContainer2" type="VBoxContainer" parent="VBoxContainer/HSplitContainer"]
margin_right = 322.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_horizontal = 3
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer2"]
margin_right = 322.0
margin_bottom = 37.0
margin_bottom = 40.0
[node name="Label" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer2/HBoxContainer2"]
margin_top = 10.0
margin_top = 11.0
margin_right = 34.0
margin_bottom = 27.0
margin_bottom = 28.0
size_flags_horizontal = 0
text = "Files"
@ -114,16 +114,16 @@ text = "Files"
unique_name_in_owner = true
margin_left = 251.0
margin_right = 322.0
margin_bottom = 37.0
margin_bottom = 40.0
rect_min_size = Vector2( 0, 32 )
size_flags_horizontal = 10
text = "Refresh"
[node name="FileTree" type="Tree" parent="VBoxContainer/HSplitContainer/VBoxContainer2"]
unique_name_in_owner = true
margin_top = 41.0
margin_top = 44.0
margin_right = 322.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_horizontal = 3
size_flags_vertical = 3
allow_rmb_select = true
@ -132,66 +132,66 @@ hide_root = true
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/HSplitContainer"]
margin_left = 334.0
margin_right = 1182.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_horizontal = 3
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer"]
margin_right = 848.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_vertical = 3
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer"]
margin_right = 848.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_horizontal = 3
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer"]
margin_right = 848.0
margin_bottom = 39.0
margin_bottom = 42.0
[node name="Label" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HBoxContainer"]
margin_top = 11.0
margin_top = 12.0
margin_right = 34.0
margin_bottom = 28.0
margin_bottom = 29.0
text = "Title:"
[node name="DocTitleLineEdit" type="LineEdit" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 38.0
margin_right = 848.0
margin_bottom = 39.0
margin_bottom = 42.0
size_flags_horizontal = 3
placeholder_text = "(Optional)"
[node name="HBoxContainer2" type="HBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer"]
margin_top = 43.0
margin_top = 46.0
margin_right = 848.0
margin_bottom = 82.0
margin_bottom = 88.0
[node name="Label" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HBoxContainer2"]
margin_top = 11.0
margin_top = 12.0
margin_right = 37.0
margin_bottom = 28.0
margin_bottom = 29.0
text = "Date:"
[node name="DocDateLineEdit" type="LineEdit" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HBoxContainer2"]
unique_name_in_owner = true
margin_left = 41.0
margin_right = 848.0
margin_bottom = 39.0
margin_bottom = 42.0
size_flags_horizontal = 3
placeholder_text = "(Optional)"
[node name="HSplitContainer" type="HSplitContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer"]
margin_top = 86.0
margin_top = 92.0
margin_right = 848.0
margin_bottom = 563.0
margin_bottom = 560.0
size_flags_vertical = 3
split_offset = 328
[node name="ContentEditContainer" type="VBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HSplitContainer"]
margin_right = 848.0
margin_bottom = 477.0
margin_bottom = 468.0
size_flags_vertical = 3
[node name="Label" type="Label" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HSplitContainer/ContentEditContainer"]
@ -203,7 +203,7 @@ text = "Content"
unique_name_in_owner = true
margin_top = 21.0
margin_right = 848.0
margin_bottom = 477.0
margin_bottom = 468.0
size_flags_horizontal = 3
size_flags_vertical = 3
show_line_numbers = true

View File

@ -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, Array], index 0 = reference to function to call, index 1 = binds to pass to func
# value: FuncRef, reference to function to call
}
var __fallback: FuncRef = null
var __server: TCP_Server = null
@ -32,7 +32,7 @@ func endpoint(type: int, endpoint: String, function: FuncRef, binds: Array = [])
)
return
__endpoints[endpoint_hash] = [function, binds]
__endpoints[endpoint_hash] = function
func fallback(function: FuncRef) -> void:
@ -121,20 +121,13 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo
var endpoint_func: FuncRef = null
var endpoint_parts: PoolStringArray = endpoint.split("/", false)
var binds
# special case for if endpoint is just root
if endpoint == "/":
var endpoint_hash: Array = [type, "/"]
if __endpoints.has(endpoint_hash):
endpoint_func = __endpoints[endpoint_hash][0]
binds = __endpoints[endpoint_hash][1]
else:
while (!endpoint_func && !endpoint_parts.empty()):
while !endpoint_func:
var endpoint_hash: Array = [type, "/" + endpoint_parts.join("/")]
if __endpoints.has(endpoint_hash):
endpoint_func = __endpoints[endpoint_hash][0]
binds = __endpoints[endpoint_hash][1]
endpoint_func = __endpoints[endpoint_hash]
elif endpoint_parts.empty():
break
else:
endpoint_parts.remove(endpoint_parts.size() - 1)
@ -160,10 +153,7 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo
"[INF] Recieved request method: %s, endpoint: %s" % [method, endpoint]
)
if !binds:
endpoint_func.call_func(request, response)
else:
endpoint_func.call_func(request, response, binds)
return response

Binary file not shown.

BIN
theme/FiraSans-Light.otf Normal file

Binary file not shown.

BIN
theme/FiraSans-Regular.otf Normal file

Binary file not shown.

View File

@ -1,6 +1,6 @@
[gd_resource type="DynamicFont" load_steps=2 format=2]
[ext_resource path="res://theme/FreeSans.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://theme/FiraSans-Regular.otf" type="DynamicFontData" id=1]
[resource]
font_data = ExtResource( 1 )

View File

@ -1,6 +1,6 @@
[gd_resource type="DynamicFont" load_steps=2 format=2]
[ext_resource path="res://theme/FreeMonoBold.ttf" type="DynamicFontData" id=1]
[ext_resource path="res://theme/FiraSans-Light.otf" type="DynamicFontData" id=1]
[resource]
size = 18

Binary file not shown.