Conversation
There was a problem hiding this comment.
Greptile Overview
Summary
This PR implements a single-port production deployment mode for Reflex applications. The change allows both the frontend and backend to run on the same port in production, which simplifies deployment architectures and is particularly beneficial for containerized environments and cloud platforms.The implementation works by introducing a new --single-port CLI flag that restricts usage to production mode only. When enabled, instead of running separate frontend and backend processes on different ports, the backend server mounts the compiled frontend static files directly using FastAPI's StaticFiles feature. This is controlled by a new internal environment variable REFLEX_MOUNT_FRONTEND_COMPILED_APP.
Key architectural changes include:
- CLI validation logic that prevents single-port mode in development (where separate ports enable hot reloading)
- Auto-detection when frontend and backend ports are explicitly set to the same value
- Backend server modification to serve static frontend assets when the environment variable is set
- Refactored console output functions for better code organization
- Port handling logic that ensures proper configuration in single-port mode
This fits well with Reflex's deployment flexibility goals and follows common web framework patterns where the backend serves pre-compiled frontend assets in production environments.
Changed Files
| Filename | Score | Overview |
|---|---|---|
| reflex/reflex.py | 4/5 | Adds single-port CLI flag with validation logic and execution flow changes |
| reflex/environment.py | 5/5 | Adds internal environment variable to control frontend mounting behavior |
| reflex/utils/exec.py | 4/5 | Implements backend mounting functionality and refactors console output |
| reflex/app.py | 4/5 | Mounts compiled frontend static files when environment variable is enabled |
Confidence score: 4/5
- This PR is safe to merge with moderate risk due to well-structured implementation of a valuable feature
- Score reflects solid architecture and validation logic, but complex deployment functionality needs thorough testing
- Pay close attention to reflex/app.py for proper static file mounting and reflex/reflex.py for CLI validation logic
Sequence Diagram
sequenceDiagram
participant User
participant CLI
participant Config
participant App
participant Environment
participant Backend
participant Frontend
User->>CLI: "reflex run --single-port --env=PROD"
CLI->>CLI: "Validate single-port constraints"
CLI->>Config: "Get current config"
CLI->>CLI: "Set backend_port = frontend_port"
CLI->>Environment: "Set environment variables"
Environment->>Environment: "REFLEX_MOUNT_FRONTEND_COMPILED_APP = True"
CLI->>App: "_run(single_port=True)"
App->>App: "setup_frontend_prod()"
App->>Backend: "run_backend_prod(mount_frontend_compiled_app=True)"
Backend->>Backend: "Mount compiled frontend at '/'"
Backend->>User: "Serve both frontend and backend on same port"
4 files reviewed, 1 comment
CodSpeed Performance ReportMerging #5839 will not alter performanceComparing Summary
|
No description provided.