From 5f51c8c478a1cb2c34e449699654d281cea4e388 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Fri, 7 Jan 2022 14:12:40 +0530 Subject: [PATCH] Fix dev server (#148) * Fix dev server * Add tests App compiles on windows but dev server doesn't work on windows at the moment --- integration_tests/conftest.py | 12 ++++++++++++ integration_tests/test_base_url.py | 7 +++++++ robyn/__init__.py | 4 ++-- src/shared_socket.rs | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py index 3e56f0279..5483be2e9 100644 --- a/integration_tests/conftest.py +++ b/integration_tests/conftest.py @@ -17,6 +17,7 @@ def session(): process.terminate() del os.environ["ROBYN_URL"] + @pytest.fixture def global_session(): os.environ["ROBYN_URL"] = "0.0.0.0" @@ -29,4 +30,15 @@ def global_session(): del os.environ["ROBYN_URL"] +@pytest.fixture +def dev_session(): + subprocess.call(["yes | freeport 5000"], shell=True) + os.environ["ROBYN_URL"] = "127.0.0.1" + current_file_path = pathlib.Path(__file__).parent.resolve() + base_routes = os.path.join(current_file_path, "./base_routes.py") + process = subprocess.Popen(["python3", base_routes, "--dev"]) + time.sleep(5) + yield + process.terminate() + del os.environ["ROBYN_URL"] diff --git a/integration_tests/test_base_url.py b/integration_tests/test_base_url.py index e17bc81a9..be6c629e3 100644 --- a/integration_tests/test_base_url.py +++ b/integration_tests/test_base_url.py @@ -6,8 +6,15 @@ def test_local_index_request(session): res = requests.get(f"{BASE_URL}") assert(res.status_code == 200) + def test_global_index_request(global_session): BASE_URL = "http://0.0.0.0:5000" res = requests.get(f"{BASE_URL}") assert(res.status_code == 200) + +def test_dev_index_request(dev_session): + BASE_URL = "http://0.0.0.0:5000" + res = requests.get(f"{BASE_URL}") + assert(res.status_code == 200) + diff --git a/robyn/__init__.py b/robyn/__init__.py index d86a03a25..2fc08c87e 100644 --- a/robyn/__init__.py +++ b/robyn/__init__.py @@ -75,9 +75,9 @@ def start(self, url="127.0.0.1", port=5000): :param port [int]: [reperesents the port number at which the server is listening] """ - socket = SocketHeld(url, port) - workers = self.workers if not self.dev: + workers = self.workers + socket = SocketHeld(url, port) for process_number in range(self.processes): copied = socket.try_clone() p = Process( diff --git a/src/shared_socket.rs b/src/shared_socket.rs index 356abcdc3..98c94e920 100644 --- a/src/shared_socket.rs +++ b/src/shared_socket.rs @@ -12,10 +12,26 @@ pub struct SocketHeld { #[pymethods] impl SocketHeld { #[new] + #[cfg(not(target_os = "windows"))] pub fn new(address: String, port: i32) -> PyResult { let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?; let address: SocketAddr = format!("{}:{}", address, port).parse()?; println!("{}", address); + socket.set_reuse_port(true)?; + socket.set_reuse_address(true)?; + socket.bind(&address.into())?; + socket.listen(1024)?; + + Ok(SocketHeld { socket }) + } + + #[new] + #[cfg(target_os = "windows")] + pub fn new(address: String, port: i32) -> PyResult { + let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?; + let address: SocketAddr = format!("{}:{}", address, port).parse()?; + println!("{}", address); + // reuse port is not available on windows socket.set_reuse_address(true)?; socket.bind(&address.into())?; socket.listen(1024)?;