瀏覽代碼

Add option to specify port (#192)

pysqz 2 年之前
父節點
當前提交
22deb9eb1b
共有 3 個文件被更改,包括 32 次插入3 次删除
  1. 3 0
      pynecone/config.py
  2. 3 1
      pynecone/constants.py
  3. 26 2
      pynecone/utils.py

+ 3 - 0
pynecone/config.py

@@ -15,6 +15,9 @@ class Config(Base):
     # The username.
     username: Optional[str] = None
 
+    # The frontend port.
+    port: str = constants.FRONTEND_PORT
+
     # The backend API url.
     api_url: str = constants.API_URL
 

+ 3 - 1
pynecone/constants.py

@@ -57,6 +57,8 @@ PCVERSION_APP_FILE = os.path.join(WEB_DIR, "pcversion.txt")
 
 
 # Commands to run the app.
+# The frontend default port.
+FRONTEND_PORT = "3000"
 # The backend api url.
 API_URL = "http://localhost:8000"
 # The default path where bun is installed.
@@ -68,7 +70,7 @@ BACKEND_HOST = "0.0.0.0"
 # The default timeout when launching the gunicorn server.
 TIMEOUT = 120
 # The command to run the backend in production mode.
-RUN_BACKEND_PROD = f"gunicorn --worker-class uvicorn.workers.UvicornH11Worker --bind 0.0.0.0:8000 --preload --timeout {TIMEOUT} --log-level critical".split()
+RUN_BACKEND_PROD = f"gunicorn --worker-class uvicorn.workers.UvicornH11Worker --preload --timeout {TIMEOUT} --log-level critical".split()
 
 # Compiler variables.
 # The extension for compiled Javascript files.

+ 26 - 2
pynecone/utils.py

@@ -14,6 +14,7 @@ import string
 import subprocess
 import sys
 import uvicorn
+from urllib.parse import urlparse
 from collections import defaultdict
 from subprocess import PIPE
 from types import ModuleType
@@ -500,7 +501,10 @@ def run_frontend(app: App):
 
     # Run the frontend in development mode.
     console.rule("[bold green]App Running")
-    subprocess.Popen([get_package_manager(), "run", "dev"], cwd=constants.WEB_DIR)
+    os.environ["PORT"] = get_config().port
+    subprocess.Popen(
+        [get_package_manager(), "run", "dev"], cwd=constants.WEB_DIR, env=os.environ
+    )
 
 
 def run_frontend_prod(app: App):
@@ -515,8 +519,12 @@ def run_frontend_prod(app: App):
     # Export the app.
     export_app(app)
 
+    os.environ["PORT"] = get_config().port
+
     # Run the frontend in production mode.
-    subprocess.Popen([get_package_manager(), "run", "prod"], cwd=constants.WEB_DIR)
+    subprocess.Popen(
+        [get_package_manager(), "run", "prod"], cwd=constants.WEB_DIR, env=os.environ
+    )
 
 
 def get_num_workers() -> int:
@@ -533,6 +541,19 @@ def get_num_workers() -> int:
     return (os.cpu_count() or 1) * 2 + 1
 
 
+def get_api_port() -> int:
+    """Get the API port.
+
+    Returns:
+        The API port.
+    """
+    port = urlparse(get_config().api_url).port
+    if port is None:
+        port = urlparse(constants.API_URL).port
+    assert port is not None
+    return port
+
+
 def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR):
     """Run the backend.
 
@@ -543,6 +564,7 @@ def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel
     uvicorn.run(
         f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
         host=constants.BACKEND_HOST,
+        port=get_api_port(),
         log_level=loglevel,
         reload=True,
     )
@@ -559,6 +581,8 @@ def run_backend_prod(
     """
     num_workers = get_num_workers()
     command = constants.RUN_BACKEND_PROD + [
+        "--bind",
+        f"0.0.0.0:{get_api_port()}",
         "--workers",
         str(num_workers),
         "--threads",