Skip to content

Commit

Permalink
Fix dev server (#148)
Browse files Browse the repository at this point in the history
* Fix dev server

* Add tests

App compiles on windows but dev server doesn't work on windows at the
moment
  • Loading branch information
sansyrox authored Jan 7, 2022
1 parent ba4aae8 commit 5f51c8c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
12 changes: 12 additions & 0 deletions integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]

7 changes: 7 additions & 0 deletions integration_tests/test_base_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

4 changes: 2 additions & 2 deletions robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions src/shared_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ pub struct SocketHeld {
#[pymethods]
impl SocketHeld {
#[new]
#[cfg(not(target_os = "windows"))]
pub fn new(address: String, port: i32) -> PyResult<SocketHeld> {
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<SocketHeld> {
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)?;
Expand Down

0 comments on commit 5f51c8c

Please sign in to comment.