Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify port #192

Merged
merged 1 commit into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pynecone/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion pynecone/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@


# 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.
Expand All @@ -68,7 +70,7 @@
# 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.
Expand Down
28 changes: 26 additions & 2 deletions pynecone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import subprocess
import sys
import uvicorn
from urllib.parse import urlparse
from collections import defaultdict
from subprocess import PIPE
from types import ModuleType
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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.

Expand All @@ -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,
)
Expand All @@ -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",
Expand Down