mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-09-11 12:17:45 +02:00
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
"""Centralized port configuration.
|
|
|
|
Reads from ports.config.json at the project root so every part of the
|
|
Python backend uses the same port numbers without hardcoding them.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
|
|
_config_path = os.path.join(os.path.dirname(__file__), "..", "ports.config.json")
|
|
with open(_config_path, encoding="utf-8") as _f:
|
|
_cfg = json.load(_f)
|
|
|
|
BACKEND_DEV_PORT: int = _cfg["backend"]["dev"]
|
|
NINE_ROUTER_DEV_PORT: int = _cfg["nineRouter"]["dev"]
|
|
NINE_ROUTER_PROD_PORT: int = _cfg["nineRouter"]["prod"]
|
|
|
|
|
|
def _is_packaged() -> bool:
|
|
return os.environ.get("OPENSWARM_PACKAGED") == "1"
|
|
|
|
|
|
def get_backend_port() -> int:
|
|
"""Return the active backend port (env override or dev default)."""
|
|
return int(os.environ.get("OPENSWARM_PORT", str(BACKEND_DEV_PORT)))
|
|
|
|
|
|
def get_nine_router_port() -> int:
|
|
"""Return the 9Router port for the current environment."""
|
|
return NINE_ROUTER_PROD_PORT if _is_packaged() else NINE_ROUTER_DEV_PORT
|
|
|
|
|
|
# Convenience alias — most callers just need the current port.
|
|
NINE_ROUTER_PORT: int = get_nine_router_port()
|