diff --git a/fastapi-vue/fastapi_vue/hostutil.py b/fastapi-vue/fastapi_vue/hostutil.py index 6b2d893..50bee9b 100644 --- a/fastapi-vue/fastapi_vue/hostutil.py +++ b/fastapi-vue/fastapi_vue/hostutil.py @@ -12,6 +12,7 @@ def parse_endpoint(value: str | None, default_port: int = 0) -> list[dict]: Returns: List of dicts with uvicorn bind kwargs (host/port or uds). + Two entries may be returned for IPv4 and IPv6 (all interaces). Supported forms: - None or empty -> [{host: "localhost", port: default_port}] @@ -65,3 +66,19 @@ def parse_endpoint(value: str | None, default_port: int = 0) -> list[dict]: ipaddress.ip_address(host) return [{"host": host, "port": port}] + + +def parse_endpoints( + listen: str | list[str] | None = None, default_port: int = 8000 +) -> list[dict]: + """Parse listen strings into a list of endpoint dicts. + + Args: + listen: Endpoint string(s) (see parse_endpoint for formats). + default_port: Port to use when not specified in listen args. + """ + if listen is None: + listen = [f"localhost:{default_port}"] + elif isinstance(listen, str): + listen = [listen] + return [ep for s in listen for ep in parse_endpoint(s, default_port)] diff --git a/fastapi-vue/fastapi_vue/server.py b/fastapi-vue/fastapi_vue/server.py index f57fceb..503112f 100644 --- a/fastapi-vue/fastapi_vue/server.py +++ b/fastapi-vue/fastapi_vue/server.py @@ -6,7 +6,7 @@ from contextlib import suppress import uvicorn from uvicorn import Config, Server -from .hostutil import parse_endpoint +from .hostutil import parse_endpoints logger = logging.getLogger(__name__) @@ -30,13 +30,9 @@ def run( workers: Number of worker processes (requires uvicorn.run, single endpoint only). **uvicorn_config: Additional uvicorn config options (overrides all other settings). """ - if listen is None: - listen = [f"localhost:{default_port}"] - elif isinstance(listen, str): - listen = [listen] - endpoints: list[dict] = [] - for ep in listen: - endpoints.extend(parse_endpoint(ep, default_port)) + endpoints = parse_endpoints(listen, default_port) + if not endpoints: + raise ValueError("No endpoints to serve; check listen configuration") conf: dict[str, object] = {"app": app, "reload": reload, "workers": workers} proxy = os.getenv("FORWARDED_ALLOW_IPS", "127.0.0.1,::1")