2022-06-21 12:48:14 +00:00
|
|
|
extends Control
|
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
var mime_types := MimeTypeHelper.generate_db()
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
var _server: HTTPServer = null
|
|
|
|
|
2022-06-25 16:37:09 +00:00
|
|
|
onready var server_ui := $ServerUI
|
|
|
|
onready var server_dir = server_ui.server_dir
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
2022-06-25 16:37:09 +00:00
|
|
|
pass
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _start_server(port: int = 3001) -> void:
|
|
|
|
if _server:
|
|
|
|
return
|
|
|
|
|
|
|
|
_server = HTTPServer.new()
|
|
|
|
var dir := Directory.new()
|
2022-06-25 16:37:09 +00:00
|
|
|
if dir.open(server_dir) == OK:
|
2022-06-25 08:56:45 +00:00
|
|
|
if dir.list_dir_begin() != OK:
|
|
|
|
# TODO: show error to user here
|
|
|
|
return
|
2022-06-21 12:48:14 +00:00
|
|
|
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
|
|
|
|
|
2022-07-01 23:11:29 +00:00
|
|
|
# print(file_name)
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
_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"])
|
|
|
|
|
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
if _server.listen(port) != OK:
|
|
|
|
# TODO: show error to user here
|
|
|
|
return
|
2022-06-21 12:48:14 +00:00
|
|
|
|
2022-06-25 16:37:09 +00:00
|
|
|
server_ui.is_server_running = true
|
|
|
|
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
func _stop_server() -> void:
|
|
|
|
if _server:
|
2022-06-25 16:37:09 +00:00
|
|
|
_server.stop()
|
2022-06-21 12:48:14 +00:00
|
|
|
_server = null
|
|
|
|
|
2022-06-25 16:37:09 +00:00
|
|
|
server_ui.is_server_running = false
|
|
|
|
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
if _server == null:
|
|
|
|
return
|
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
if not _server.take_connection():
|
|
|
|
# TODO: show error to user here
|
|
|
|
return
|
2022-06-21 12:48:14 +00:00
|
|
|
|
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
|
2022-06-21 12:48:14 +00:00
|
|
|
var file_name: String = binds[0] as String
|
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
var f := File.new()
|
|
|
|
var success = f.open("res://server_files/%s" % file_name, File.READ)
|
2022-06-21 12:48:14 +00:00
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
if success == OK:
|
|
|
|
var mime := mime_types.get(file_name)
|
2022-07-01 23:11:29 +00:00
|
|
|
response.type(mime)
|
2022-06-25 14:37:26 +00:00
|
|
|
|
2022-07-01 23:11:29 +00:00
|
|
|
var data = f.get_buffer(f.get_len())
|
2022-06-21 12:48:14 +00:00
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
response.data(data)
|
2022-06-25 14:37:26 +00:00
|
|
|
|
2022-06-25 08:56:45 +00:00
|
|
|
else:
|
|
|
|
response.header("content-type", "text/plain")
|
|
|
|
response.data("500 - Read Error")
|
2022-06-25 16:37:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|