AppHarness: set config frontend_port and backend_port

This commit is contained in:
Masen Furer 2024-11-07 20:43:11 -08:00
parent 81d1c0c5b0
commit 13d21ad4d5
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -44,6 +44,7 @@ import reflex.utils.format
import reflex.utils.prerequisites
import reflex.utils.processes
from reflex.config import environment
from reflex.proxy import proxy_middleware
from reflex.state import (
BaseState,
StateManager,
@ -297,6 +298,9 @@ class AppHarness:
self.state_manager = StateManagerRedis.create(self.app_instance.state)
else:
self.state_manager = self.app_instance._state_manager
# Disable proxy for app harness tests.
if proxy_middleware in self.app_instance.lifespan_tasks:
self.app_instance.lifespan_tasks.remove(proxy_middleware)
def _reload_state_module(self):
"""Reload the rx.State module to avoid conflict when reloading."""
@ -364,9 +368,12 @@ class AppHarness:
def _start_frontend(self):
# Set up the frontend.
with chdir(self.app_path):
backend_host, backend_port = self._poll_for_servers().getsockname()
config = reflex.config.get_config()
config.backend_port = backend_port
config.api_url = "http://{0}:{1}".format(
*self._poll_for_servers().getsockname(),
backend_host,
backend_port,
)
reflex.utils.build.setup_frontend(self.app_path)
@ -391,6 +398,7 @@ class AppHarness:
self.frontend_url = m.group(1)
config = reflex.config.get_config()
config.deploy_url = self.frontend_url
config.frontend_port = int(self.frontend_url.rpartition(":")[2])
break
if self.frontend_url is None:
raise RuntimeError("Frontend did not start")
@ -916,17 +924,20 @@ class AppHarnessProd(AppHarness):
root=web_root,
error_page_map=error_page_map,
) as self.frontend_server:
self.frontend_url = "http://localhost:{1}".format(
*self.frontend_server.socket.getsockname()
)
config = reflex.config.get_config()
config.frontend_port = self.frontend_server.server_address[1]
self.frontend_url = f"http://localhost:{config.frontend_port}"
self.frontend_server.serve_forever()
def _start_frontend(self):
# Set up the frontend.
with chdir(self.app_path):
backend_host, backend_port = self._poll_for_servers().getsockname()
config = reflex.config.get_config()
config.backend_port = backend_port
config.api_url = "http://{0}:{1}".format(
*self._poll_for_servers().getsockname(),
backend_host,
backend_port,
)
reflex.reflex.export(
zipping=False,