diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index a5511ef..aea04d4 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -326,7 +326,14 @@ class AppResponse: if self._ptr == ffi.NULL: self._ptr = ffi.new_handle(self) lib.uws_res_on_aborted(self.SSL, self.res, uws_generic_abord_handler, self._ptr) - + + def redirect(self, location, status_code=302): + if not isinstance(location, str): + raise RuntimeError("Location must be an string") + self.write_status(status_code) + self.write_header("Location", location) + self.end_without_body() + def end(self, message, end_connection=False): if not self.aborted: if isinstance(message, str): diff --git a/tests/examples/requeriments.txt b/tests/examples/requeriments.txt index 2d6e0bb..63ccd82 100644 --- a/tests/examples/requeriments.txt +++ b/tests/examples/requeriments.txt @@ -1,2 +1,3 @@ aiohttp -redis \ No newline at end of file +redis +git+https://github.com/cirospaciari/socketify.py.git@main#socketify --global-option="build_ext" \ No newline at end of file diff --git a/tests/examples/router_and_basics.py b/tests/examples/router_and_basics.py index 762971b..a5d9168 100644 --- a/tests/examples/router_and_basics.py +++ b/tests/examples/router_and_basics.py @@ -56,6 +56,15 @@ def send_in_parts(res, req): res.write("messages") res.end(" in parts!") + +def redirect(res, req): + #status code is optional default is 302 + res.redirect("/redirected", 302) + +def redirected(res, req): + res.end("You got redirected to here :D") + + def not_found(res, req): res.write_status(404).end("Not Found") @@ -69,6 +78,8 @@ app.get("/json", json) app.get("/sleepy", sleepy_json) app.get("/custom_header", custom_header) app.get("/send_in_parts", send_in_parts) +app.get("/redirect", redirect) +app.get("/redirected", redirected) # Wildcard at last always :) app.any("/*", not_found)