__init__.py 2.5 KB

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