49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
|
|
from flask import Flask, jsonify
|
||
|
|
from flask_cors import CORS
|
||
|
|
from flask_jwt_extended import JWTManager
|
||
|
|
from flask_sqlalchemy import SQLAlchemy
|
||
|
|
from flask_migrate import Migrate
|
||
|
|
import os
|
||
|
|
|
||
|
|
# Create extensions but don't initialize them yet
|
||
|
|
db = SQLAlchemy()
|
||
|
|
migrate = Migrate()
|
||
|
|
jwt = JWTManager()
|
||
|
|
cors = CORS()
|
||
|
|
|
||
|
|
|
||
|
|
def create_app(config_name=None):
|
||
|
|
"""Application factory pattern"""
|
||
|
|
app = Flask(__name__)
|
||
|
|
|
||
|
|
# Load configuration
|
||
|
|
if config_name is None:
|
||
|
|
config_name = os.environ.get('FLASK_ENV', 'development')
|
||
|
|
|
||
|
|
from app.config import config_by_name
|
||
|
|
app.config.from_object(config_by_name[config_name])
|
||
|
|
|
||
|
|
# Initialize extensions with app
|
||
|
|
db.init_app(app)
|
||
|
|
migrate.init_app(app, db)
|
||
|
|
jwt.init_app(app)
|
||
|
|
cors.init_app(app, resources={r"/api/*": {"origins": app.config.get('CORS_ORIGINS', '*')}})
|
||
|
|
|
||
|
|
# Import models (required for migrations)
|
||
|
|
from app.models import user, product, order
|
||
|
|
|
||
|
|
# Register blueprints
|
||
|
|
from app.routes import api_bp, health_bp
|
||
|
|
app.register_blueprint(api_bp, url_prefix='/api')
|
||
|
|
app.register_blueprint(health_bp)
|
||
|
|
|
||
|
|
# Global error handlers
|
||
|
|
@app.errorhandler(404)
|
||
|
|
def not_found(error):
|
||
|
|
return jsonify({'error': 'Not found'}), 404
|
||
|
|
|
||
|
|
@app.errorhandler(500)
|
||
|
|
def internal_error(error):
|
||
|
|
return jsonify({'error': 'Internal server error'}), 500
|
||
|
|
|
||
|
|
return app
|