__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import os
  2. from flask import Flask, send_from_directory
  3. from flask_sqlalchemy import SQLAlchemy
  4. from flask_migrate import Migrate
  5. from flask_cors import CORS
  6. from config.settings import config
  7. db = SQLAlchemy()
  8. migrate = Migrate()
  9. def create_app(config_name=None):
  10. """Application factory pattern"""
  11. if config_name is None:
  12. config_name = os.environ.get('FLASK_ENV', 'development')
  13. # Check if static folder exists (for Docker deployment)
  14. static_folder = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static')
  15. if os.path.exists(static_folder):
  16. app = Flask(__name__, static_folder=static_folder, static_url_path='')
  17. else:
  18. app = Flask(__name__)
  19. app.config.from_object(config[config_name])
  20. # Initialize extensions
  21. db.init_app(app)
  22. migrate.init_app(app, db)
  23. CORS(app)
  24. # Create upload directories
  25. os.makedirs(app.config.get('UPLOAD_FOLDER', 'uploads'), exist_ok=True)
  26. os.makedirs(app.config.get('REPORTS_FOLDER', 'reports'), exist_ok=True)
  27. # Register blueprints
  28. from app.api import api_bp
  29. app.register_blueprint(api_bp, url_prefix='/api')
  30. # Register error handlers
  31. from app.errors import register_error_handlers
  32. register_error_handlers(app)
  33. # Serve frontend static files (for Docker deployment)
  34. if app.static_folder and os.path.exists(app.static_folder):
  35. @app.route('/')
  36. def serve_index():
  37. return send_from_directory(app.static_folder, 'index.html')
  38. @app.route('/<path:path>')
  39. def serve_static(path):
  40. # If file exists, serve it; otherwise serve index.html for SPA routing
  41. file_path = os.path.join(app.static_folder, path)
  42. if os.path.exists(file_path) and os.path.isfile(file_path):
  43. return send_from_directory(app.static_folder, path)
  44. return send_from_directory(app.static_folder, 'index.html')
  45. return app