make the endpoints at request time
This commit is contained in:
parent
836c101e6f
commit
3d2fd37191
31
Main.gd
31
Main.gd
@ -17,26 +17,8 @@ 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"), ["index.html"]) # this is still needed to make 'localhost:port/' to point to index.html
|
||||
_server.fallback(funcref(self, "_serve_file"))
|
||||
|
||||
if _server.listen(port) != OK:
|
||||
# TODO: show error to user here
|
||||
@ -64,11 +46,11 @@ func _process(_delta: float) -> void:
|
||||
|
||||
func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, binds: Array) -> void:
|
||||
var file_name: String = binds[0] as String
|
||||
|
||||
# TODO: deprecate binds in favor of using the Request.endpoint prop, as for this app's purposes it would be the same as the file name
|
||||
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:
|
||||
|
@ -160,10 +160,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)
|
||||
endpoint_func.call_func(request, response, binds if binds else [request.endpoint()])
|
||||
|
||||
return response
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user