- Updated `run.py` to conditionally print startup information only in the reloader process to avoid duplicate logs in debug mode. - Modified `__init__.py` to log startup and completion messages based on the reloader process condition. - Added warnings suppression in `graph_builder.py` for Pydantic v2 regarding Field usage. - Revised `ontology_generator.py` to enforce strict design guidelines for entity types and relationships, ensuring compliance with new requirements. - Improved logging behavior in `logger.py` to prevent log propagation to the root logger, avoiding duplicate outputs.
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
"""
|
|
MiroFish Backend - Flask应用工厂
|
|
"""
|
|
|
|
import os
|
|
from flask import Flask, request
|
|
from flask_cors import CORS
|
|
|
|
from .config import Config
|
|
from .utils.logger import setup_logger, get_logger
|
|
|
|
|
|
def create_app(config_class=Config):
|
|
"""Flask应用工厂函数"""
|
|
app = Flask(__name__)
|
|
app.config.from_object(config_class)
|
|
|
|
# 设置日志
|
|
logger = setup_logger('mirofish')
|
|
|
|
# 只在 reloader 子进程中打印启动信息(避免 debug 模式下打印两次)
|
|
is_reloader_process = os.environ.get('WERKZEUG_RUN_MAIN') == 'true'
|
|
debug_mode = app.config.get('DEBUG', False)
|
|
should_log_startup = not debug_mode or is_reloader_process
|
|
|
|
if should_log_startup:
|
|
logger.info("=" * 50)
|
|
logger.info("MiroFish Backend 启动中...")
|
|
logger.info("=" * 50)
|
|
|
|
# 启用CORS
|
|
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
|
|
|
# 请求日志中间件
|
|
@app.before_request
|
|
def log_request():
|
|
logger = get_logger('mirofish.request')
|
|
logger.debug(f"请求: {request.method} {request.path}")
|
|
if request.content_type and 'json' in request.content_type:
|
|
logger.debug(f"请求体: {request.get_json(silent=True)}")
|
|
|
|
@app.after_request
|
|
def log_response(response):
|
|
logger = get_logger('mirofish.request')
|
|
logger.debug(f"响应: {response.status_code}")
|
|
return response
|
|
|
|
# 注册蓝图
|
|
from .api import graph_bp
|
|
app.register_blueprint(graph_bp, url_prefix='/api/graph')
|
|
|
|
# 健康检查
|
|
@app.route('/health')
|
|
def health():
|
|
return {'status': 'ok', 'service': 'MiroFish Backend'}
|
|
|
|
if should_log_startup:
|
|
logger.info("MiroFish Backend 启动完成")
|
|
|
|
return app
|
|
|