Skip to content

Commit

Permalink
feat: flexible app port (#632)
Browse files Browse the repository at this point in the history
* feat: flexible app port

Set app port either with `PORT` env variable or with `--port` CLI argument

* fix: linting
  • Loading branch information
frederik-encord authored Sep 7, 2023
1 parent 2292f44 commit c7e0be1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/encord_active/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,22 @@ def start(
target: Path = typer.Option(
Path.cwd(), "--target", "-t", help="Path of the project you would like to start", file_okay=False
),
port: int = typer.Option(8000, help="Bind app to this port", envvar="PORT"),
):
"""
[green bold]Launch[/green bold] the application with the provided project ✨
"""
from encord_active.cli.utils.server import launch_server_app

launch_server_app(target)
launch_server_app(target, port)


@cli.command()
def quickstart(
target: Path = typer.Option(
Path.cwd(), "--target", "-t", help="Directory where the project would be saved.", file_okay=False
),
port: int = typer.Option(8000, help="Bind app to this port", envvar="PORT"),
):
"""
[green bold]Start[/green bold] Encord Active straight away 🏃💨
Expand All @@ -437,7 +439,7 @@ def quickstart(
project_dir.mkdir(exist_ok=True)

fetch_prebuilt_project(project_name, project_dir)
launch_server_app(project_dir)
launch_server_app(project_dir, port)


@cli.command(rich_help_panel="Resources")
Expand Down
12 changes: 7 additions & 5 deletions src/encord_active/cli/utils/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ def is_port_in_use(port: int) -> bool:
return s.connect_ex(("localhost", port)) == 0


def launch_server_app(target: Path):
if is_port_in_use(8000):
def launch_server_app(target: Path, port: int):
if is_port_in_use(port):
import typer

rich.print("[red]Port already in use...")
rich.print(
f"[orange1]Port [blue]{port}[/blue] already in use. Try changing the `[blue]--port[/blue]` option.[/orange1]"
)
raise typer.Exit()
else:
rich.print("[yellow]Bear with us, this might take a short while...")
Expand All @@ -51,5 +53,5 @@ def launch_server_app(target: Path):
generate_prisma_client()
ensure_safe_project(target)
data_dir = target.expanduser().absolute()
rich.print("[green] Server starting on localhost:8000")
start(data_dir, app_config.is_dev)
rich.print(f"[green] Server starting on [blue]http://localhost:{port}[/blue]")
start(data_dir, port=port, reload=app_config.is_dev)
7 changes: 4 additions & 3 deletions src/encord_active/server/start_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
from pathlib import Path


def start(path: Path, reload=False):
def start(path: Path, port: int, reload=False):
from uvicorn import run

environ["SERVER_START_PATH"] = path.as_posix()
opts = {"reload": reload, "port": environ.get("PORT"), "host": environ.get("HOST")}
opts = {"reload": reload, "port": port, "host": environ.get("HOST")}
if reload:
server_watch_path = Path(__file__).parent.parent
opts["reload_dirs"] = [server_watch_path.resolve().as_posix()]
opts = {k: v for k, v in opts.items() if v is not None}
environ.setdefault("API_URL", f"http://localhost:{port}")
run("encord_active.server.main:app", **opts)


if __name__ == "__main__":
start(Path(sys.argv[1]), reload=("--reload" in sys.argv))
start(Path(sys.argv[1]), port=8000, reload=("--reload" in sys.argv))

0 comments on commit c7e0be1

Please sign in to comment.