瀏覽代碼

WiP: in prod mode, export frontend as static files

Masen Furer 1 年之前
父節點
當前提交
40ed16a765
共有 2 個文件被更改,包括 27 次插入3 次删除
  1. 7 0
      reflex/app.py
  2. 20 3
      reflex/reflex.py

+ 7 - 0
reflex/app.py

@@ -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:

+ 20 - 3
reflex/reflex.py

@@ -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()