65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
"""修补基础平台 app.py,注册 insurance Blueprint。
|
||
|
||
在 Docker 构建时执行,将注册代码注入到 app.py 中。
|
||
"""
|
||
APP_PY = "/app/api/app.py"
|
||
|
||
INSURANCE_FUNC = (
|
||
"\n# ====== Insurance Blueprint Auto-Register ======\n"
|
||
"def _register_insurance(app):\n"
|
||
' """在基础平台 app 创建后注册 insurance Blueprint。"""\n'
|
||
" import os\n"
|
||
" # 加载保险模块配置到 Flask app.config\n"
|
||
" for key in ['BAODAN_CHAT_API_KEY', 'BAODAN_WORKFLOW_API_KEY', 'BAODAN_API_URL',\n"
|
||
" 'BAODAN_KB_API_KEY', 'WECOM_CORP_ID', 'WECOM_SECRET',\n"
|
||
" 'WECOM_TOKEN', 'WECOM_AES_KEY', 'WECOM_WEBHOOK_URL',\n"
|
||
" 'JWT_SECRET', 'JWT_EXPIRE_SECONDS', 'GUEST_MODE']:\n"
|
||
" if key in os.environ:\n"
|
||
" app.config[key] = os.environ[key]\n"
|
||
" try:\n"
|
||
" from insurance.routes import register_insurance_routes\n"
|
||
" register_insurance_routes(app)\n"
|
||
" except Exception as e:\n"
|
||
" import logging\n"
|
||
' logging.warning(f"Insurance module not loaded: {e}")\n'
|
||
"# ====== END Insurance ======\n\n"
|
||
)
|
||
|
||
|
||
def patch_app_py():
|
||
with open(APP_PY, "r") as f:
|
||
content = f.read()
|
||
|
||
if "register_insurance_routes" in content:
|
||
print("[patch_app.py] Already patched, skipping.")
|
||
return
|
||
|
||
# 1. 在文件开头插入 _register_insurance 函数定义
|
||
# 插入到 "def is_db_command" 之前
|
||
marker = "\ndef is_db_command"
|
||
if marker in content:
|
||
content = content.replace(marker, "\n" + INSURANCE_FUNC + "def is_db_command", 1)
|
||
|
||
# 2. db 命令分支:app 创建后注册
|
||
content = content.replace(
|
||
" app = create_migrations_app()\n socketio_app = app",
|
||
" app = create_migrations_app()\n _register_insurance(app)\n socketio_app = app",
|
||
1,
|
||
)
|
||
|
||
# 3. 正常启动分支:flask_app 创建后注册
|
||
content = content.replace(
|
||
" socketio_app, flask_app = create_app()\n app = flask_app",
|
||
" socketio_app, flask_app = create_app()\n _register_insurance(flask_app)\n app = flask_app",
|
||
1,
|
||
)
|
||
|
||
with open(APP_PY, "w") as f:
|
||
f.write(content)
|
||
|
||
print("[patch_app.py] Patched app.py with insurance Blueprint registration.")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
patch_app_py()
|