加入一个全局Client ,方便所有地方调用。
This commit is contained in:
@@ -4,78 +4,17 @@ import logging
|
||||
import threading
|
||||
import time
|
||||
from argparse import ArgumentParser
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from gewechat_client import GewechatClient
|
||||
|
||||
import socket
|
||||
# 启动FastAPI服务器
|
||||
# 从callback_url中提取主机和端口
|
||||
import urllib.parse
|
||||
|
||||
from configuration import Config
|
||||
from constants import ChatType
|
||||
from gewechat.api.start_server import start_fastapi_server
|
||||
from robot import Robot
|
||||
from gewechat.api.callback import router as callback_router
|
||||
|
||||
# 配置日志
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_port_in_use(port, host='0.0.0.0'):
|
||||
"""检查端口是否被占用"""
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
try:
|
||||
s.bind((host, port))
|
||||
return False
|
||||
except socket.error:
|
||||
return True
|
||||
|
||||
|
||||
def start_fastapi_server(host="0.0.0.0", port=8999):
|
||||
"""启动FastAPI服务器"""
|
||||
# 检查端口是否被占用
|
||||
if is_port_in_use(port, host):
|
||||
logger.warning(f"端口 {port} 已被占用,尝试使用其他端口")
|
||||
# 尝试其他端口
|
||||
for test_port in range(9000, 9100):
|
||||
if not is_port_in_use(test_port, host):
|
||||
port = test_port
|
||||
break
|
||||
else:
|
||||
logger.error("无法找到可用端口,服务器启动失败")
|
||||
return False
|
||||
|
||||
try:
|
||||
app = FastAPI()
|
||||
app.include_router(callback_router)
|
||||
|
||||
# 添加健康检查路由
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "ok"}
|
||||
|
||||
logger.info(f"正在启动FastAPI服务器,地址: http://{host}:{port}")
|
||||
|
||||
# 使用线程启动uvicorn服务器
|
||||
server_thread = threading.Thread(
|
||||
target=uvicorn.run,
|
||||
args=(app,),
|
||||
kwargs={"host": host, "port": port, "log_level": "info"},
|
||||
daemon=True
|
||||
)
|
||||
server_thread.start()
|
||||
logger.info(f"FastAPI 服务已在 http://{host}:{port} 启动")
|
||||
logger.info(f"回调URL: http://{host}:{port}/gewechat/callback")
|
||||
|
||||
# 返回启动的端口,以便调用者知道实际使用的端口
|
||||
return port
|
||||
except Exception as e:
|
||||
logger.error(f"启动FastAPI服务器失败: {e}", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
def main(chat_type: int):
|
||||
config = Config()
|
||||
base_url = config.BASE_URL
|
||||
@@ -99,8 +38,8 @@ def main(chat_type: int):
|
||||
if error_msg:
|
||||
logger.error("登录失败")
|
||||
return
|
||||
#休眠等待server启动,防止回调设置失败
|
||||
time.sleep(10)
|
||||
# 休眠等待server启动,防止回调设置失败
|
||||
time.sleep(5)
|
||||
resp = client.set_callback(token, callback_url)
|
||||
print(f"set_callback:{resp}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user