__init__.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from flask import Flask
  2. from flask_cors import CORS
  3. import logging
  4. from app.config.config import Config
  5. def create_app(config_class=Config):
  6. """Create and configure the Flask application"""
  7. app = Flask(__name__)
  8. app.config.from_object(config_class)
  9. # Enable CORS
  10. CORS(
  11. app,
  12. resources={
  13. r"/api/*": { # 使用更简洁的API路径
  14. "origins": "*",
  15. "methods": ["GET", "POST", "PUT", "DELETE"],
  16. "allow_headers": ["Content-Type"]
  17. }
  18. },
  19. supports_credentials=True
  20. )
  21. # Configure logging
  22. configure_logging(app)
  23. # 输出启动信息
  24. app.logger.info(f"Starting server on port {config_class.PORT}")
  25. # 导入并注册API蓝图
  26. from app.api.meta_data import bp as meta_data_bp
  27. from app.api.data_resource import bp as data_resource_bp
  28. from app.api.data_model import bp as data_model_bp
  29. from app.api.data_interface import bp as data_interface_bp
  30. from app.api.data_metric import bp as data_metric_bp
  31. from app.api.production_line import bp as production_line_bp
  32. from app.api.graph import bp as graph_bp
  33. from app.api.system import bp as system_bp
  34. # 使用更简洁的API前缀
  35. app.register_blueprint(meta_data_bp, url_prefix='/api/meta')
  36. app.register_blueprint(data_resource_bp, url_prefix='/api/resource')
  37. app.register_blueprint(data_model_bp, url_prefix='/api/model')
  38. app.register_blueprint(data_interface_bp, url_prefix='/api/interface')
  39. app.register_blueprint(data_metric_bp, url_prefix='/api/metric')
  40. app.register_blueprint(production_line_bp, url_prefix='/api/pipeline')
  41. app.register_blueprint(graph_bp, url_prefix='/api/graph')
  42. app.register_blueprint(system_bp, url_prefix='/api/system')
  43. return app
  44. def configure_logging(app):
  45. """Configure logging for the application"""
  46. logger = logging.getLogger("app")
  47. handler = logging.FileHandler('flask.log', encoding='UTF-8')
  48. handler.setLevel(logging.INFO)
  49. logging_format = logging.Formatter(
  50. '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
  51. handler.setFormatter(logging_format)
  52. console = logging.StreamHandler()
  53. console.setLevel(logging.INFO)
  54. console.setFormatter(logging_format)
  55. logger.addHandler(handler)
  56. logger.addHandler(console)
  57. return logger