2026-04-14 10:08:41 +08:00

66 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
直接启动服务端 - 绕过 uvicorn 命令行日志问题
确保所有日志实时输出到终端
"""
import sys
import os
import uvicorn
from pathlib import Path
# 确保项目根目录在 sys.path
ROOT = Path(__file__).resolve().parent.parent # scripts 的上一级
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
# ✅ 关键修复:切换到项目根目录,确保 models/ 等相对路径正确
os.chdir(ROOT)
# 强制配置日志输出到标准输出
import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s | %(levelname)-8s | %(name)s:%(funcName)s:%(lineno)d | %(message)s',
stream=sys.stdout,
)
# 导入 FastAPI 应用与配置(端口与 .env 一致,避免与 WS_PORT 漂移)
from app.main import app
from app.config import settings
def main():
"""启动服务"""
# WebSocket 保活:协议级 Ping/Pong由 websockets 库处理,不占用应用层 JSON
ws_ping_interval = 20.0 # 秒;空闲时定期发 Ping避免 NAT/代理 idle 断连
ws_ping_timeout = 30.0 # 秒;未收到对端 Pong 则关闭连接
print("\n" + "=" * 60)
print(" 🚀 云端无人机语音服务 - 直接启动模式")
print("=" * 60)
print(f" 监听地址: ws://{settings.WS_HOST}:{settings.WS_PORT}{settings.WS_PATH}")
print(f" 健康检查: http://127.0.0.1:{settings.WS_PORT}/health")
print(f" 日志级别: DEBUG")
print(f" WS 保活: Ping {ws_ping_interval:g}s / Pong 超时 {ws_ping_timeout:g}s")
print("=" * 60 + "\n")
print("⏳ 等待客户端连接... (按 Ctrl+C 停止)\n")
# 使用 uvicorn 启动,但强制日志输出到 stdout
config = uvicorn.Config(
app,
host=settings.WS_HOST,
port=settings.WS_PORT,
log_level="info", # ✅ 改为 info屏蔽掉底层协议 (DEBUG) 的海量刷屏
log_config=None,
use_colors=True,
ws_ping_interval=ws_ping_interval,
ws_ping_timeout=ws_ping_timeout,
)
server = uvicorn.Server(config)
server.run()
if __name__ == "__main__":
main()