| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import os
- from flask import Flask, send_from_directory
- from flask_sqlalchemy import SQLAlchemy
- from flask_migrate import Migrate
- from flask_cors import CORS
- from config.settings import config
- db = SQLAlchemy()
- migrate = Migrate()
- def create_app(config_name=None):
- """Application factory pattern"""
- if config_name is None:
- config_name = os.environ.get('FLASK_ENV', 'development')
-
- # Check if static folder exists (for Docker deployment)
- static_folder = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'static')
- if os.path.exists(static_folder):
- app = Flask(__name__, static_folder=static_folder, static_url_path='')
- else:
- app = Flask(__name__)
-
- app.config.from_object(config[config_name])
-
- # Initialize extensions
- db.init_app(app)
- migrate.init_app(app, db)
- CORS(app)
-
- # Create upload directories
- os.makedirs(app.config.get('UPLOAD_FOLDER', 'uploads'), exist_ok=True)
- os.makedirs(app.config.get('REPORTS_FOLDER', 'reports'), exist_ok=True)
-
- # Register blueprints
- from app.api import api_bp
- app.register_blueprint(api_bp, url_prefix='/api')
-
- # Register error handlers
- from app.errors import register_error_handlers
- register_error_handlers(app)
-
- # Serve frontend static files (for Docker deployment)
- if app.static_folder and os.path.exists(app.static_folder):
- @app.route('/')
- def serve_index():
- return send_from_directory(app.static_folder, 'index.html')
-
- @app.route('/<path:path>')
- def serve_static(path):
- # If file exists, serve it; otherwise serve index.html for SPA routing
- file_path = os.path.join(app.static_folder, path)
- if os.path.exists(file_path) and os.path.isfile(file_path):
- return send_from_directory(app.static_folder, path)
- return send_from_directory(app.static_folder, 'index.html')
-
- return app
|