add server start functionality to ServerUI

This commit is contained in:
Lera Elvoé 2022-06-25 19:37:09 +03:00
parent 80bc3e4a11
commit 550a1c80a4
6 changed files with 53 additions and 10 deletions

21
Main.gd
View File

@ -4,11 +4,12 @@ var mime_types := MimeTypeHelper.generate_db()
var _server: HTTPServer = null var _server: HTTPServer = null
var files: Array = [] onready var server_ui := $ServerUI
onready var server_dir = server_ui.server_dir
func _ready() -> void: func _ready() -> void:
_start_server() pass
func _start_server(port: int = 3001) -> void: func _start_server(port: int = 3001) -> void:
@ -17,7 +18,7 @@ func _start_server(port: int = 3001) -> void:
_server = HTTPServer.new() _server = HTTPServer.new()
var dir := Directory.new() var dir := Directory.new()
if dir.open("res://server_files") == OK: if dir.open(server_dir) == OK:
if dir.list_dir_begin() != OK: if dir.list_dir_begin() != OK:
# TODO: show error to user here # TODO: show error to user here
return return
@ -41,11 +42,16 @@ func _start_server(port: int = 3001) -> void:
# TODO: show error to user here # TODO: show error to user here
return return
server_ui.is_server_running = true
func _stop_server() -> void: func _stop_server() -> void:
if _server: if _server:
_server.stop()
_server = null _server = null
server_ui.is_server_running = false
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
if _server == null: if _server == null:
@ -77,3 +83,12 @@ func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, bi
else: else:
response.header("content-type", "text/plain") response.header("content-type", "text/plain")
response.data("500 - Read Error") response.data("500 - Read Error")
func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void:
server_dir = new_dir
_start_server(port)
func _on_ServerUI_stop_server_button_pressed() -> void:
_stop_server()

View File

@ -9,3 +9,6 @@ anchor_bottom = 1.0
script = ExtResource( 1 ) script = ExtResource( 1 )
[node name="ServerUI" parent="." instance=ExtResource( 2 )] [node name="ServerUI" parent="." instance=ExtResource( 2 )]
[connection signal="start_server_button_pressed" from="ServerUI" to="." method="_on_ServerUI_start_server_button_pressed"]
[connection signal="stop_server_button_pressed" from="ServerUI" to="." method="_on_ServerUI_stop_server_button_pressed"]

View File

@ -17,6 +17,7 @@ enum ContextMenuOptions {
onready var server_path_label := $"%ServerPathLabel" onready var server_path_label := $"%ServerPathLabel"
onready var port_spin_box := $"%PortSpinBox" onready var port_spin_box := $"%PortSpinBox"
onready var start_server_button := $"%StartServerButton"
onready var server_status_label := $"%ServerStatusLabel" onready var server_status_label := $"%ServerStatusLabel"
onready var document_title_lineedit := $"%DocTitleLineEdit" onready var document_title_lineedit := $"%DocTitleLineEdit"
@ -32,7 +33,8 @@ onready var server_folder_dialog := $"%ServerFolderDialog"
signal server_folder_changed(new_path) # new_path: String signal server_folder_changed(new_path) # new_path: String
signal server_port_changed(new_port) # new_port: int signal server_port_changed(new_port) # new_port: int
signal start_server_button_pressed(port) # port: int signal start_server_button_pressed(port, path) # port: int, path: String
signal stop_server_button_pressed() # emit from %StartServerButton, when the server is not running.
signal open_browser_button_pressed(port) # port: int signal open_browser_button_pressed(port) # port: int
signal files_selection_changed(new_files) # new_files: Array<String> signal files_selection_changed(new_files) # new_files: Array<String>
@ -147,7 +149,21 @@ func _on_OpenServerFolderButton_pressed() -> void:
func set_server_running(running: bool) -> void: func set_server_running(running: bool) -> void:
is_server_running = running # TODO: logic for disabling and enabling certain nodes is_server_running = running
if is_server_running:
server_status_label.text = SERVER_STATUS_TEXT.RUNNING
start_server_button.text = "Stop server"
port_spin_box.editable = false
document_date_lineedit.editable = false
document_title_lineedit.editable = false
document_input_textedit.readonly = true
else:
server_status_label.text = SERVER_STATUS_TEXT.NOT_RUNNING
start_server_button.text = "Start server"
port_spin_box.editable = !false
document_date_lineedit.editable = !false
document_title_lineedit.editable = !false
document_input_textedit.readonly = !true
func _generate_filestxt(): func _generate_filestxt():
@ -163,7 +179,15 @@ func _generate_filestxt():
f.store_string(files) f.store_string(files)
f.close() f.close()
else: else:
push_error("File.txt open for save error!") push_error("File.txt open for save error!") # TODO: show a user-facing error
func _on_StartServerButton_pressed() -> void:
if is_server_running:
emit_signal("stop_server_button_pressed")
else:
_generate_filestxt()
emit_signal("start_server_button_pressed", port_spin_box.value, server_dir)
func _is_file_in_filestxt(path: String) -> bool: func _is_file_in_filestxt(path: String) -> bool:

View File

@ -62,6 +62,7 @@ max_value = 8000.0
value = 3001.0 value = 3001.0
[node name="StartServerButton" type="Button" parent="VBoxContainer/HBoxContainer"] [node name="StartServerButton" type="Button" parent="VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
margin_left = 693.0 margin_left = 693.0
margin_right = 778.0 margin_right = 778.0
margin_bottom = 32.0 margin_bottom = 32.0
@ -246,6 +247,7 @@ rect_min_size = Vector2( 95, 56 )
items = [ "Move Up", null, 0, false, false, 0, 0, null, "", false, "Move Down", null, 0, false, false, 1, 0, null, "", false ] items = [ "Move Up", null, 0, false, false, 0, 0, null, "", false, "Move Down", null, 0, false, false, 1, 0, null, "", false ]
[connection signal="pressed" from="VBoxContainer/HBoxContainer/OpenServerFolderButton" to="." method="_on_OpenServerFolderButton_pressed"] [connection signal="pressed" from="VBoxContainer/HBoxContainer/OpenServerFolderButton" to="." method="_on_OpenServerFolderButton_pressed"]
[connection signal="pressed" from="VBoxContainer/HBoxContainer/StartServerButton" to="." method="_on_StartServerButton_pressed"]
[connection signal="item_edited" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_edited"] [connection signal="item_edited" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_edited"]
[connection signal="item_rmb_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_rmb_selected"] [connection signal="item_rmb_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_rmb_selected"]
[connection signal="item_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_selected"] [connection signal="item_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_selected"]

View File

@ -1,3 +1,2 @@
first.md 2022-01-06 first article first.md 2022-03-03 first title
second.md 2021-05-03 second article second.md 2022-03-05 second title
second.md