From c7e0be150c6b394b6cc30ceb76d491049ad67615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Hvilsh=C3=B8j?= <93145535+frederik-encord@users.noreply.github.com> Date: Thu, 7 Sep 2023 13:54:03 +0200 Subject: [PATCH] feat: flexible app port (#632) * feat: flexible app port Set app port either with `PORT` env variable or with `--port` CLI argument * fix: linting --- src/encord_active/cli/main.py | 6 ++++-- src/encord_active/cli/utils/server.py | 12 +++++++----- src/encord_active/server/start_server.py | 7 ++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/encord_active/cli/main.py b/src/encord_active/cli/main.py index 61847c0e6..5ddb5187d 100644 --- a/src/encord_active/cli/main.py +++ b/src/encord_active/cli/main.py @@ -411,13 +411,14 @@ 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() @@ -425,6 +426,7 @@ 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 🏃💨 @@ -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") diff --git a/src/encord_active/cli/utils/server.py b/src/encord_active/cli/utils/server.py index 17c09b878..f6d3ce78c 100644 --- a/src/encord_active/cli/utils/server.py +++ b/src/encord_active/cli/utils/server.py @@ -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...") @@ -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) diff --git a/src/encord_active/server/start_server.py b/src/encord_active/server/start_server.py index 158d18395..03ddcd8ab 100644 --- a/src/encord_active/server/start_server.py +++ b/src/encord_active/server/start_server.py @@ -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))