Compare commits
No commits in common. "3437aa0abdec143eb85a1c5d3e9a5db9e867ccd1" and "e80043bd6b8d1d18e769e19abb3a788f84d4d0ab" have entirely different histories.
3437aa0abd
...
e80043bd6b
3
.gitignore
vendored
3
.gitignore
vendored
@ -9,6 +9,3 @@ export_presets.cfg
|
|||||||
# Mono-specific ignores
|
# Mono-specific ignores
|
||||||
.mono/
|
.mono/
|
||||||
data_*/
|
data_*/
|
||||||
|
|
||||||
# VSCode config folder
|
|
||||||
.vscode/
|
|
||||||
|
35
Main.gd
35
Main.gd
@ -17,7 +17,26 @@ func _start_server(port: int = 3001) -> void:
|
|||||||
return
|
return
|
||||||
|
|
||||||
_server = HTTPServer.new()
|
_server = HTTPServer.new()
|
||||||
_server.endpoint(HTTPServer.Method.GET, "/", funcref(self, "_serve_file"))
|
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"])
|
||||||
|
|
||||||
|
|
||||||
if _server.listen(port) != OK:
|
if _server.listen(port) != OK:
|
||||||
# TODO: show error to user here
|
# TODO: show error to user here
|
||||||
@ -43,14 +62,13 @@ func _process(_delta: float) -> void:
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
func _serve_file(request: HTTPServer.Request, response: HTTPServer.Response) -> void:
|
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
|
||||||
var file_name: String = request.endpoint()
|
var file_name: String = binds[0] as String
|
||||||
if file_name == "/": # if the request is for root, serve index
|
|
||||||
file_name = "index.html"
|
|
||||||
var f := File.new()
|
var f := File.new()
|
||||||
var success = f.open(server_dir.plus_file(file_name), File.READ)
|
var success = f.open(server_dir.plus_file(file_name), File.READ)
|
||||||
|
|
||||||
if success == OK: # TODO: handle other errors like file not found
|
if success == OK:
|
||||||
var mime := mime_types.get(file_name)
|
var mime := mime_types.get(file_name)
|
||||||
response.type(mime)
|
response.type(mime)
|
||||||
|
|
||||||
@ -59,9 +77,8 @@ func _serve_file(request: HTTPServer.Request, response: HTTPServer.Response) ->
|
|||||||
response.data(data)
|
response.data(data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response.type(mime_types.get("txt"))
|
response.header("content-type", "text/plain")
|
||||||
response.status(500)
|
response.data("500 - Read Error")
|
||||||
response.data("Internal Server Error")
|
|
||||||
|
|
||||||
|
|
||||||
func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void:
|
func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void:
|
||||||
|
@ -13,7 +13,7 @@ const Status = preload("res://addons/http_server/status.gd")
|
|||||||
|
|
||||||
var __endpoints: Dictionary = {
|
var __endpoints: Dictionary = {
|
||||||
# key: [Int, String], array with 0 index representing method, 1 index representing endpoint
|
# key: [Int, String], array with 0 index representing method, 1 index representing endpoint
|
||||||
# value: FuncRef, reference to function to call
|
# value: [FuncRef, Array], index 0 = reference to function to call, index 1 = binds to pass to func
|
||||||
}
|
}
|
||||||
var __fallback: FuncRef = null
|
var __fallback: FuncRef = null
|
||||||
var __server: TCP_Server = null
|
var __server: TCP_Server = null
|
||||||
@ -32,7 +32,7 @@ func endpoint(type: int, endpoint: String, function: FuncRef, binds: Array = [])
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
__endpoints[endpoint_hash] = function
|
__endpoints[endpoint_hash] = [function, binds]
|
||||||
|
|
||||||
|
|
||||||
func fallback(function: FuncRef) -> void:
|
func fallback(function: FuncRef) -> void:
|
||||||
@ -121,15 +121,22 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo
|
|||||||
|
|
||||||
var endpoint_func: FuncRef = null
|
var endpoint_func: FuncRef = null
|
||||||
var endpoint_parts: PoolStringArray = endpoint.split("/", false)
|
var endpoint_parts: PoolStringArray = endpoint.split("/", false)
|
||||||
|
var binds
|
||||||
|
|
||||||
while !endpoint_func:
|
# special case for if endpoint is just root
|
||||||
var endpoint_hash: Array = [type, "/" + endpoint_parts.join("/")]
|
if endpoint == "/":
|
||||||
|
var endpoint_hash: Array = [type, "/"]
|
||||||
if __endpoints.has(endpoint_hash):
|
if __endpoints.has(endpoint_hash):
|
||||||
endpoint_func = __endpoints[endpoint_hash]
|
endpoint_func = __endpoints[endpoint_hash][0]
|
||||||
elif endpoint_parts.empty():
|
binds = __endpoints[endpoint_hash][1]
|
||||||
break
|
else:
|
||||||
else:
|
while (!endpoint_func && !endpoint_parts.empty()):
|
||||||
endpoint_parts.remove(endpoint_parts.size() - 1)
|
var endpoint_hash: Array = [type, "/" + endpoint_parts.join("/")]
|
||||||
|
if __endpoints.has(endpoint_hash):
|
||||||
|
endpoint_func = __endpoints[endpoint_hash][0]
|
||||||
|
binds = __endpoints[endpoint_hash][1]
|
||||||
|
else:
|
||||||
|
endpoint_parts.remove(endpoint_parts.size() - 1)
|
||||||
|
|
||||||
|
|
||||||
if !endpoint_func:
|
if !endpoint_func:
|
||||||
@ -153,7 +160,10 @@ func __process_request(method: String, endpoint: String, headers: Dictionary, bo
|
|||||||
"[INF] Recieved request method: %s, endpoint: %s" % [method, endpoint]
|
"[INF] Recieved request method: %s, endpoint: %s" % [method, endpoint]
|
||||||
)
|
)
|
||||||
|
|
||||||
endpoint_func.call_func(request, response)
|
if !binds:
|
||||||
|
endpoint_func.call_func(request, response)
|
||||||
|
else:
|
||||||
|
endpoint_func.call_func(request, response, binds)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user