feat: add --api-host to bincio dev and --mobile to dev_test.py for local mobile testing
This commit is contained in:
+24
-5
@@ -68,7 +68,18 @@ def _merge_all_users(data: Path) -> None:
|
||||
_write_root_manifest(data)
|
||||
|
||||
|
||||
def _start_serve(data: Path, api_port: int, site: Path) -> None:
|
||||
def _local_ip() -> str:
|
||||
"""Return the machine's primary LAN IP (best-effort)."""
|
||||
import socket
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
s.connect(("8.8.8.8", 80))
|
||||
return s.getsockname()[0]
|
||||
except Exception:
|
||||
return "127.0.0.1"
|
||||
|
||||
|
||||
def _start_serve(data: Path, api_port: int, site: Path, api_host: str = "127.0.0.1") -> None:
|
||||
"""Start bincio serve in a background thread."""
|
||||
import uvicorn
|
||||
import bincio.serve.server as srv
|
||||
@@ -78,7 +89,7 @@ def _start_serve(data: Path, api_port: int, site: Path) -> None:
|
||||
|
||||
config = uvicorn.Config(
|
||||
srv.app,
|
||||
host="127.0.0.1",
|
||||
host=api_host,
|
||||
port=api_port,
|
||||
log_level="warning", # quiet — astro dev output takes priority
|
||||
)
|
||||
@@ -147,11 +158,14 @@ def _watch_data(data: Path) -> None:
|
||||
@click.option("--site-dir", default=None, help="Astro project directory (default: ./site)")
|
||||
@click.option("--port", default=4321, show_default=True, help="Astro dev server port")
|
||||
@click.option("--api-port", default=4041, show_default=True, help="bincio serve API port")
|
||||
@click.option("--api-host", default="127.0.0.1", show_default=True,
|
||||
help="Host for bincio serve. Use 0.0.0.0 to expose on the local network for mobile testing.")
|
||||
def dev(
|
||||
data_dir: Optional[str],
|
||||
site_dir: Optional[str],
|
||||
port: int,
|
||||
api_port: int,
|
||||
api_host: str,
|
||||
) -> None:
|
||||
"""Start the local dev environment: bincio serve + astro dev.
|
||||
|
||||
@@ -169,11 +183,16 @@ def dev(
|
||||
|
||||
has_auth = (data / "instance.db").exists()
|
||||
|
||||
lan_ip = _local_ip() if api_host == "0.0.0.0" else api_host
|
||||
mobile_url = f"http://{lan_ip}:{api_port}" if api_host == "0.0.0.0" else None
|
||||
|
||||
console.print(f"[bold]bincio dev[/bold]")
|
||||
console.print(f" Data: [cyan]{data}[/cyan]")
|
||||
console.print(f" Site: [cyan]{site}[/cyan]")
|
||||
if has_auth:
|
||||
console.print(f" API: [cyan]http://127.0.0.1:{api_port}[/cyan]")
|
||||
console.print(f" API: [cyan]http://{api_host}:{api_port}[/cyan]")
|
||||
if mobile_url:
|
||||
console.print(f" Mobile: [bold cyan]{mobile_url}[/bold cyan] ← set this as instance URL in the app")
|
||||
else:
|
||||
console.print(f" Auth: [yellow]none[/yellow] (single-user, no instance.db)")
|
||||
console.print(f" Browser: [cyan]http://localhost:{port}[/cyan]")
|
||||
@@ -196,8 +215,8 @@ def dev(
|
||||
|
||||
# Start bincio serve only when instance.db exists (auth / write API)
|
||||
if has_auth:
|
||||
console.print(f"Starting [cyan]bincio serve[/cyan] on port {api_port}…")
|
||||
t = threading.Thread(target=_start_serve, args=(data, api_port, site), daemon=True)
|
||||
console.print(f"Starting [cyan]bincio serve[/cyan] on {api_host}:{api_port}…")
|
||||
t = threading.Thread(target=_start_serve, args=(data, api_port, site, api_host), daemon=True)
|
||||
t.start()
|
||||
|
||||
# Watch data dir for sidecar/activity changes → auto-merge
|
||||
|
||||
Reference in New Issue
Block a user