Files
opc-manager/backend/flask_app.py
mac caebf90438 重构:flask_app.py 拆分为 db/helpers/routes/seed_data + Blueprint
- flask_app.py 1166行→33行纯入口
- 新建 db.py(配置+连接+SQL工具)
- 新建 helpers.py(attach_common/monthly_finance/add_file_index)
- 新建 routes.py(全路由 Blueprint + 装饰器 + TABLES)
- 新建 migrations/seed_data.py(seed_db 搬迁)
- migrations/{tables,columns,data_fixes,seed}.py 改 import 为 from db
- 删除死代码 init_db(228行)+ latest_followup(10行)
- 反向依赖消除:migrations 不再 import flask_app
- 前端零改动,URL 不变
2026-07-02 18:30:24 +08:00

34 lines
1.0 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.
# flask_app.py — 入口app 创建 + 蓝图注册 + 迁移 + 启动
# 拆分后仅 ~35 行,所有路由在 routes.py业务辅助在 helpers.py基础设施在 db.py
import os
import sys
# 确保 backend 目录在 sys.path 中(兼容 gunicorn --preload 模式)
_backend_dir = os.path.dirname(os.path.abspath(__file__))
if _backend_dir not in sys.path:
sys.path.insert(0, _backend_dir)
from flask import Flask
from db import ROOT # 触发 load_dotenv + 建目录
from routes import bp
from migrations import run_migrations
app = Flask(
__name__,
template_folder=str(ROOT / "templates"),
static_folder=str(ROOT / "static"),
)
app.secret_key = os.environ.get("SECRET_KEY", "opc-dev-secret-2026")
app.register_blueprint(bp)
# 模块级执行迁移(保留 gunicorn --preload 行为:导入即触发)
run_migrations()
if __name__ == "__main__":
app.run(
host="127.0.0.1",
port=5177,
debug=os.environ.get("FLASK_DEBUG", "false").lower() in ("true", "1", "yes"),
)