diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index 1c4374e..c5918b7 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -240,6 +240,8 @@ def uws_generic_listen_handler(listen_socket, config, user_data): app = ffi.from_handle(user_data) if hasattr(app, "_listen_handler") and hasattr(app._listen_handler, '__call__'): app.socket = listen_socket + if listen_socket == ffi.NULL: + raise RuntimeError("Failed to listen on port %d" % int(config.port)) app._listen_handler(None if config == ffi.NULL else AppListenOptions(port=int(config.port),host=None if config.host == ffi.NULL else ffi.string(config.host).decode("utf-8"), options=int(config.options))) @ffi.callback("void(uws_res_t *, void*)") diff --git a/tests/async.py b/tests/examples/async.py similarity index 81% rename from tests/async.py rename to tests/examples/async.py index 1772074..f1adcdd 100644 --- a/tests/async.py +++ b/tests/examples/async.py @@ -8,7 +8,7 @@ async def delayed_hello(delay, res): res.end("Hello with delay!") def home(res, req): - #request objecy only lives during the live time of this call + #request object only lives during the life time of this call #get parameters, query, headers anything you need here delay = req.get_query("delay") delay = 0 if delay == None else float(delay) @@ -16,15 +16,19 @@ def home(res, req): #abort handler is grabed here, so responses only will be send if res.aborted == False res.run_async(delayed_hello(delay, res)) -async def json_message(res, req): +async def json(res, _): #req maybe will not be available in direct attached async functions #but if you dont care about req info you can do it await asyncio.sleep(2) #do something async res.write_header("Content-Type", "application/json") res.end({ "message": "I'm delayed!"}) +def not_found(res, req): + res.write_status(404).end("Not Found") + app.get("/", home) -app.get("/json_message", json_message) +app.get("/json", json) +app.any("/*", not_found) app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port))) app.run() \ No newline at end of file diff --git a/tests/docker/pypy3/Dockerfile b/tests/examples/docker/pypy3/Dockerfile similarity index 100% rename from tests/docker/pypy3/Dockerfile rename to tests/examples/docker/pypy3/Dockerfile diff --git a/tests/docker/pypy3/main.py b/tests/examples/docker/pypy3/main.py similarity index 100% rename from tests/docker/pypy3/main.py rename to tests/examples/docker/pypy3/main.py diff --git a/tests/docker/pypy3/requirements.txt b/tests/examples/docker/pypy3/requirements.txt similarity index 100% rename from tests/docker/pypy3/requirements.txt rename to tests/examples/docker/pypy3/requirements.txt diff --git a/tests/docker/python3-alpine/Dockerfile b/tests/examples/docker/python3-alpine/Dockerfile similarity index 100% rename from tests/docker/python3-alpine/Dockerfile rename to tests/examples/docker/python3-alpine/Dockerfile diff --git a/tests/docker/python3-alpine/main.py b/tests/examples/docker/python3-alpine/main.py similarity index 100% rename from tests/docker/python3-alpine/main.py rename to tests/examples/docker/python3-alpine/main.py diff --git a/tests/docker/python3-alpine/requirements.txt b/tests/examples/docker/python3-alpine/requirements.txt similarity index 100% rename from tests/docker/python3-alpine/requirements.txt rename to tests/examples/docker/python3-alpine/requirements.txt diff --git a/tests/docker/python3/Dockerfile b/tests/examples/docker/python3/Dockerfile similarity index 100% rename from tests/docker/python3/Dockerfile rename to tests/examples/docker/python3/Dockerfile diff --git a/tests/docker/python3/main.py b/tests/examples/docker/python3/main.py similarity index 100% rename from tests/docker/python3/main.py rename to tests/examples/docker/python3/main.py diff --git a/tests/docker/python3/requirements.txt b/tests/examples/docker/python3/requirements.txt similarity index 100% rename from tests/docker/python3/requirements.txt rename to tests/examples/docker/python3/requirements.txt diff --git a/tests/shutdown.py b/tests/examples/graceful_shutdown.py similarity index 100% rename from tests/shutdown.py rename to tests/examples/graceful_shutdown.py diff --git a/tests/examples/not_found.py b/tests/examples/not_found.py new file mode 100644 index 0000000..ef51983 --- /dev/null +++ b/tests/examples/not_found.py @@ -0,0 +1,24 @@ +from socketify import App, AppOptions, AppListenOptions + +app = App() + +def home(res, req): + res.end("Hello, World!") + +def user(res, req): + try: + if int(req.get_parameter(0)) == 1: + return res.end("Hello user 1!") + finally: + #invalid user tells to go, to the next route valid route (not found) + req.set_yield(1) + +def not_found(res, req): + res.write_status(404).end("Not Found") + +app.get("/", home) +app.get("/user/:user_id", user) +app.any("/*", not_found) + +app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port))) +app.run() \ No newline at end of file