refactor: 将端口配置从硬编码改为从配置文件读取

将应用端口从硬编码的8000改为从config.py读取PORT配置,提高配置灵活性
更新了README.md和keyring.example.yaml以说明端口配置
同时在standalone.py中也使用配置的端口值
This commit is contained in:
Yuyao Huang (Sam) 2026-03-29 15:39:45 +08:00
parent 4ea15d4612
commit b36acf65ca
5 changed files with 24 additions and 3 deletions

View File

@ -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

View File

@ -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)]

View File

@ -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

View File

@ -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,

View File

@ -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,