zhoudoe23's picture
Update app.py
eb26aa5 verified
Raw
History Blame Contribute Delete
1.95 kB
import os
import sys
import time
import subprocess
import threading
import yaml
from fastapi import FastAPI
app = FastAPI()
def run_lichess_bot():
token = os.environ.get("LICHESS_TOKEN")
if not token:
print("[Error] 未找到 LICHESS_TOKEN 环境变量,Bot 无法启动!", file=sys.stderr)
return
bot_dir = "/app/lichess-bot"
config_path = os.path.join(bot_dir, "config.yml")
# 生成 lichess-bot 配置文件
config_data = {
"token": token,
"url": "https://lichess.org/",
"engine": {
"dir": "/app",
"name": "engine.py",
"protocol": "uci"
},
"challenge": {
"min_increment": 0,
"max_increment": 180,
"min_initial": 60,
"max_initial": 3600,
"time_controls": ["bullet", "blitz", "rapid", "classical"],
"modes": ["casual", "rated"],
"variants": ["standard"],
"accept_bot": True
},
"matchmaking": {
"allow_matchmaking": True,
"allow_during_games": False,
"challenge_timeout": 30,
"challenge_variant": "standard",
"challenge_initial_time": [60,120,180,300,600,900,1800],
"challenge_increment": [0,1,2,3,4,5],
"challenge_mode": "rated",
"challenge_filter": "fine"
}
}
with open(config_path, "w") as f:
yaml.dump(config_data, f)
print("[App] 正在启动 lichess-bot 主程序...", file=sys.stderr)
# 在后台持续运行 lichess-bot
subprocess.run(["python3", "lichess-bot.py"], cwd=bot_dir)
# 在独立线程中启动 Bot,防止阻塞 FastAPI Web 服务
@app.on_event("startup")
def startup_event():
thread = threading.Thread(target=run_lichess_bot, daemon=True)
thread.start()
@app.get("/")
def health_check():
return {"status": "running", "bot": "ChessQween3-base"}