@
修复:路径可移植性、密钥持久化、补充缺失API - 硬编码路径改为环境变量 + 项目相对路径 - secret_key 持久化到 .secret_key 文件,避免重启失效 - 新增 /api/status 路由,支持前端轮询状态 - 新增 /api/reset 路由,支持前端重置会话 - .gitignore 添加 .secret_key 排除 @
This commit is contained in:
@@ -15,6 +15,7 @@ env/
|
|||||||
# --- Environment & Config ---
|
# --- Environment & Config ---
|
||||||
.env
|
.env
|
||||||
.user.ini
|
.user.ini
|
||||||
|
.secret_key
|
||||||
|
|
||||||
# --- Logs ---
|
# --- Logs ---
|
||||||
logs/
|
logs/
|
||||||
|
|||||||
@@ -17,10 +17,9 @@ import requests
|
|||||||
from flask import Flask, render_template, request, jsonify, send_file
|
from flask import Flask, render_template, request, jsonify, send_file
|
||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
# 配置日志
|
# 配置日志 — 优先使用环境变量,否则使用项目相对路径
|
||||||
LOG_DIR = '/www/wwwlogs/douyin_cookie'
|
LOG_DIR = os.environ.get('LOG_DIR', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs'))
|
||||||
if not os.path.exists(LOG_DIR):
|
os.makedirs(LOG_DIR, exist_ok=True)
|
||||||
os.makedirs(LOG_DIR, exist_ok=True)
|
|
||||||
|
|
||||||
handler = RotatingFileHandler(
|
handler = RotatingFileHandler(
|
||||||
f'{LOG_DIR}/app.log',
|
f'{LOG_DIR}/app.log',
|
||||||
@@ -33,12 +32,22 @@ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = os.urandom(24)
|
# 持久化 secret_key:优先使用环境变量,否则从文件读取,都不存在则生成并保存
|
||||||
|
_secret_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.secret_key')
|
||||||
|
_secret_key = os.environ.get('SECRET_KEY')
|
||||||
|
if not _secret_key and os.path.exists(_secret_key_path):
|
||||||
|
with open(_secret_key_path, 'rb') as _f:
|
||||||
|
_secret_key = _f.read()
|
||||||
|
if not _secret_key:
|
||||||
|
_secret_key = os.urandom(24)
|
||||||
|
with open(_secret_key_path, 'wb') as _f:
|
||||||
|
_f.write(_secret_key)
|
||||||
|
app.secret_key = _secret_key
|
||||||
app.logger.addHandler(handler)
|
app.logger.addHandler(handler)
|
||||||
app.logger.setLevel(logging.INFO)
|
app.logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
# 静态文件目录
|
# 静态文件目录 — 优先使用环境变量,否则使用项目相对路径
|
||||||
STATIC_DIR = '/www/wwwroot/douyin_cookie_extractor/static'
|
STATIC_DIR = os.environ.get('STATIC_DIR', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'))
|
||||||
os.makedirs(STATIC_DIR, exist_ok=True)
|
os.makedirs(STATIC_DIR, exist_ok=True)
|
||||||
|
|
||||||
# 全局状态
|
# 全局状态
|
||||||
@@ -434,5 +443,32 @@ def test_proxy():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"status": "error", "message": str(e)})
|
return jsonify({"status": "error", "message": str(e)})
|
||||||
|
|
||||||
|
@app.route('/api/status', methods=['GET'])
|
||||||
|
def api_status():
|
||||||
|
"""返回当前会话状态,供前端轮询"""
|
||||||
|
elapsed = ""
|
||||||
|
if login_session.status in ("qr_ready", "scanning", "success") and login_session.start_time:
|
||||||
|
elapsed_sec = int(time.time() - login_session.start_time)
|
||||||
|
elapsed = f" (已等待 {elapsed_sec}s)"
|
||||||
|
return jsonify({
|
||||||
|
"status": login_session.status,
|
||||||
|
"message": login_session.message + elapsed,
|
||||||
|
"proxy_used": login_session.proxy_used
|
||||||
|
})
|
||||||
|
|
||||||
|
@app.route('/api/reset', methods=['POST'])
|
||||||
|
def api_reset():
|
||||||
|
"""强制重置当前会话"""
|
||||||
|
try:
|
||||||
|
run_async_sync(cleanup_session())
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.warning(f"重置会话异常: {e}")
|
||||||
|
login_session.status = "idle"
|
||||||
|
login_session.message = "会话已重置"
|
||||||
|
login_session.cookies = None
|
||||||
|
login_session.start_time = 0
|
||||||
|
login_session.proxy_used = None
|
||||||
|
return jsonify({"status": "success", "message": "会话已重置"})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=False, host='0.0.0.0', port=5001)
|
app.run(debug=False, host='0.0.0.0', port=5001)
|
||||||
Reference in New Issue
Block a user