123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- from flask import Flask
- from flask_cors import CORS
- import logging
- from app.config.config import Config
- def create_app(config_class=Config):
- """Create and configure the Flask application"""
- app = Flask(__name__)
- app.config.from_object(config_class)
-
- # Enable CORS
- CORS(
- app,
- resources={
- r"/api/*": { # 使用更简洁的API路径
- "origins": "*",
- "methods": ["GET", "POST", "PUT", "DELETE"],
- "allow_headers": ["Content-Type"]
- }
- },
- supports_credentials=True
- )
-
- # Configure logging
- configure_logging(app)
-
- # 输出启动信息
- app.logger.info(f"Starting server on port {config_class.PORT}")
-
- # 导入并注册API蓝图
- from app.api.meta_data import bp as meta_data_bp
- from app.api.data_resource import bp as data_resource_bp
- from app.api.data_model import bp as data_model_bp
- from app.api.data_interface import bp as data_interface_bp
- from app.api.data_metric import bp as data_metric_bp
- from app.api.production_line import bp as production_line_bp
- from app.api.graph import bp as graph_bp
- from app.api.system import bp as system_bp
-
- # 使用更简洁的API前缀
- app.register_blueprint(meta_data_bp, url_prefix='/api/meta')
- app.register_blueprint(data_resource_bp, url_prefix='/api/resource')
- app.register_blueprint(data_model_bp, url_prefix='/api/model')
- app.register_blueprint(data_interface_bp, url_prefix='/api/interface')
- app.register_blueprint(data_metric_bp, url_prefix='/api/metric')
- app.register_blueprint(production_line_bp, url_prefix='/api/pipeline')
- app.register_blueprint(graph_bp, url_prefix='/api/graph')
- app.register_blueprint(system_bp, url_prefix='/api/system')
-
- return app
- def configure_logging(app):
- """Configure logging for the application"""
- logger = logging.getLogger("app")
- handler = logging.FileHandler('flask.log', encoding='UTF-8')
- handler.setLevel(logging.INFO)
- logging_format = logging.Formatter(
- '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
- handler.setFormatter(logging_format)
- console = logging.StreamHandler()
- console.setLevel(logging.INFO)
- console.setFormatter(logging_format)
- logger.addHandler(handler)
- logger.addHandler(console)
-
- return logger
|