From b36acf65cae6f639eb6478cf91ca1e622c06585c Mon Sep 17 00:00:00 2001 From: "Yuyao Huang (Sam)" Date: Sun, 29 Mar 2026 15:39:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BB=8E=E7=A1=AC=E7=BC=96=E7=A0=81=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E4=BB=8E=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将应用端口从硬编码的8000改为从config.py读取PORT配置,提高配置灵活性 更新了README.md和keyring.example.yaml以说明端口配置 同时在standalone.py中也使用配置的端口值 --- README.md | 5 +++++ config.py | 3 +++ keyring.example.yaml | 8 ++++++++ main.py | 3 ++- standalone.py | 8 ++++++-- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5ad6ea4..34ec76e 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,11 @@ OPENAI_BASE_URL: https://open.bigmodel.cn/api/paas/v4/ OPENAI_API_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx OPENAI_MODEL: glm-4.7 +# Server configuration +# Only used in router mode (python main.py) or standalone mode (python standalone.py) +# Default: 8000 +PORT: 8000 + # Root directory for all project sessions (absolute path) # Only used in standalone mode (python standalone.py) # In router mode (python main.py), this field is ignored diff --git a/config.py b/config.py index 67e717c..d48f123 100644 --- a/config.py +++ b/config.py @@ -23,6 +23,9 @@ METASO_API_KEY: str = _cfg.get("METASO_API_KEY", "") ROUTER_MODE: bool = _cfg.get("ROUTER_MODE", False) ROUTER_SECRET: str = _cfg.get("ROUTER_SECRET", "") +# Server configuration +PORT: int = _cfg.get("PORT", 8000) + _allowed_open_ids_raw = _cfg.get("ALLOWED_OPEN_IDS", []) ALLOWED_OPEN_IDS: list[str] = _allowed_open_ids_raw if isinstance(_allowed_open_ids_raw, list) else [str(_allowed_open_ids_raw)] diff --git a/keyring.example.yaml b/keyring.example.yaml index 30e7983..bd64b49 100644 --- a/keyring.example.yaml +++ b/keyring.example.yaml @@ -1,3 +1,11 @@ +# Server configuration +# Only used in router mode (python main.py) or standalone mode (python standalone.py) +# Default: 8000 +PORT: 8000 + +# Root directory for all project sessions (absolute path) +# Only used in standalone mode (python standalone.py) +# In router mode (python main.py), this field is ignored WORKING_DIR: "/path/to/working/directory" FEISHU_APP_ID: your_feishu_app_id FEISHU_APP_SECRET: your_feishu_app_secret diff --git a/main.py b/main.py index e8535d9..cfad67b 100644 --- a/main.py +++ b/main.py @@ -105,10 +105,11 @@ async def shutdown_event() -> None: if __name__ == "__main__": + from config import PORT uvicorn.run( "main:app", host="0.0.0.0", - port=8000, + port=PORT, reload=False, log_level="info", ws_ping_interval=20, diff --git a/standalone.py b/standalone.py index eb5e8cd..4637ccb 100644 --- a/standalone.py +++ b/standalone.py @@ -24,7 +24,8 @@ logger = logging.getLogger(__name__) async def run_standalone() -> None: """Run router + host client in a single process.""" secret = secrets.token_hex(16) - router_url = "ws://127.0.0.1:8000/ws/node" + from config import PORT + router_url = f"ws://127.0.0.1:{PORT}/ws/node" from router.main import create_app from host_client.main import NodeClient @@ -34,12 +35,15 @@ async def run_standalone() -> None: config.router_url = router_url config.router_secret = secret + import uvicorn + from config import PORT + app = create_app(router_secret=secret) config_obj = uvicorn.Config( app, host="0.0.0.0", - port=8000, + port=PORT, log_level="info", ws_ping_interval=20, ws_ping_timeout=60,