WiP: in prod mode, export frontend as static files

This commit is contained in:
Masen Furer 2024-05-14 11:09:13 -07:00
parent 1e9ccecea0
commit 40ed16a765
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95
2 changed files with 27 additions and 3 deletions

View File

@ -212,6 +212,13 @@ class App(Base):
# Set up the admin dash.
self._setup_admin_dash()
if os.environ.get(constants.ENV_MODE_ENV_VAR) == constants.Env.PROD.value:
static_dir = constants.Dirs.WEB_STATIC
if os.path.exists(static_dir):
self.api.mount(
"/", StaticFiles(directory=static_dir, html=True), name="frontend"
)
def _enable_state(self) -> None:
"""Enable state for the app."""
if not self.state:

View File

@ -132,6 +132,7 @@ def _run(
backend_port: str = str(config.backend_port),
backend_host: str = config.backend_host,
loglevel: constants.LogLevel = config.loglevel,
export: bool = False,
):
"""Run the app in the given directory."""
from reflex.utils import build, exec, prerequisites, processes
@ -179,7 +180,7 @@ def _run(
if frontend:
# Get the app module.
prerequisites.get_compiled_app()
prerequisites.get_compiled_app(export=export and env == constants.Env.PROD)
# Warn if schema is not up to date.
prerequisites.check_schema_up_to_date()
@ -212,7 +213,11 @@ def _run(
# Run the frontend on a separate thread.
if frontend:
setup_frontend(Path.cwd())
commands.append((frontend_cmd, Path.cwd(), frontend_port, backend))
if export and env == constants.Env.PROD:
# In prod mode, export the frontend and serve it via static files.
build.export(backend=False, frontend=True, zip=False)
else:
commands.append((frontend_cmd, Path.cwd(), frontend_port, backend))
# In prod mode, run the backend on a separate thread.
if backend and env == constants.Env.PROD:
@ -251,9 +256,21 @@ def run(
loglevel: constants.LogLevel = typer.Option(
config.loglevel, help="The log level to use."
),
export: bool = typer.Option(
False, help="Export the frontend first in production mode."
),
):
"""Run the app in the current directory."""
_run(env, frontend, backend, frontend_port, backend_port, backend_host, loglevel)
_run(
env,
frontend,
backend,
frontend_port,
backend_port,
backend_host,
loglevel,
export,
)
@cli.command()