refactor(backend): 迁移基础设施,core,拆分models

This commit is contained in:
sophie committed 2026-06-25 14:01:52 +08:00
1 parent b89d93d738
commit 053ea392f2
61 files changed
+3329 -3268

No files matched your search

+12 -44
View File
@@ -1,21 +1,21 @@
"""
backend/app/__init__.py
应用工厂:创建 Flask 应用、注册蓝图并挂载全局异常与响应处理。
用途:应用工厂:创建 Flask 应用、挂载全局异常与响应处理。
依赖:app.extensions(db、migrate)、app.api.register_blueprints、app.core.config。
逻辑:create_app 初始化扩展后委托 api 层集中注册 Blueprint;models/services 仍可通过 from app import db 引用。
"""
import logging
import uuid
from flask import Flask, jsonify, request, g
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_cors import CORS
from werkzeug.exceptions import HTTPException
from app.config import config
from app.errors import AppError
from app.response_utils import error_payload, utc_iso_now
db = SQLAlchemy()
migrate = Migrate()
from app.core.config.settings import config
from app.core.errors import AppError
from app.extensions import db, migrate
from app.core.response import error_payload, utc_iso_now
logger = logging.getLogger(__name__)
@@ -27,7 +27,7 @@ def _apply_persisted_settings(app: Flask) -> None:
common case where the table does not yet exist (fresh deploy before
``flask db upgrade`` runs).
"""
from app.env_persistence import apply_env_settings
from app.core.config.env_persistence import apply_env_settings
try:
applied = apply_env_settings()
@@ -46,42 +46,9 @@ def create_app(config_name='default'):
migrate.init_app(app, db)
CORS(app) # 允许跨域
# 注册 Blueprints
from app.api.chat import chat_bp
from app.api.admin import admin_bp
from app.api.agents import agents_bp
from app.api.user import user_bp
from app.api.knowledge_base import knowledge_base_bp
from app.api.integration import integration_bp
from app.api.schedule_admin import schedule_admin_bp
from app.api.workspace_schedule import workspace_schedule_bp
from app.api.workspace_shipping_notice import workspace_shipping_notice_bp
from app.api.workspace_file_check import workspace_file_check_bp
from app.api.internal_jobs import internal_jobs_bp
from app.api.shipping_notice_tools import shipping_notice_bp
from app.api.file_check_tools import file_check_bp
from app.api.speech import speech_bp
from app.api import register_blueprints
# 聊天相关接口
app.register_blueprint(chat_bp, url_prefix='/api/chat')
# 管理员相关接口
app.register_blueprint(admin_bp, url_prefix='/api/admin')
# 普通用户可见的 Agent 列表接口
app.register_blueprint(agents_bp, url_prefix='/api/agents')
# 当前用户相关接口(首页、常见问题等)
app.register_blueprint(user_bp, url_prefix='/api/user')
# 知识库数据清洗模块接口
app.register_blueprint(knowledge_base_bp, url_prefix='/api/kb')
# 知识库外部集成接口(供外部系统按 Dify 映射反查源文件)
app.register_blueprint(integration_bp, url_prefix='/api/integration')
app.register_blueprint(schedule_admin_bp, url_prefix='/api/admin')
app.register_blueprint(workspace_schedule_bp, url_prefix='/api/workspace')
app.register_blueprint(workspace_shipping_notice_bp, url_prefix='/api/workspace')
app.register_blueprint(workspace_file_check_bp, url_prefix='/api/workspace')
app.register_blueprint(internal_jobs_bp, url_prefix='/api/internal/jobs')
app.register_blueprint(shipping_notice_bp, url_prefix='/api/shipping-notice')
app.register_blueprint(file_check_bp, url_prefix='/api/file-check')
app.register_blueprint(speech_bp, url_prefix='/api/speech')
register_blueprints(app)
# Eagerly load persisted runtime settings so the very first request after
# a worker boot already sees the admin-saved configuration (otherwise the
@@ -193,6 +160,7 @@ def create_app(config_name='default'):
@app.errorhandler(AppError)
def _handle_app_error(exc: AppError):
"""统一业务异常 → JSON error_payload(API 层应 raise AppError 而非 jsonify error)。"""
_log_api_error(
code=exc.code,
message=exc.message,