| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- """Application factory."""
- from flask import Flask
- from app.config.setting import config
- from app.extensions import db
- def register_blueprints(app):
- """Register all application blueprints."""
- from app.api.v1 import create_blueprint_v1
- from app.routes.health import bp as health_bp
- # API v1 under /v1
- app.register_blueprint(create_blueprint_v1(), url_prefix="/v1")
- # Health endpoint at /health
- app.register_blueprint(health_bp)
- def create_app(env: str = "default"):
- """Create and configure the Flask application."""
- app = Flask(__name__)
- app.config.from_object(config[env])
- # initialize extensions
- db.init_app(app)
- # optional: initialize logging and other extensions here
- try:
- from app.utils.logger import setup_logging
- setup_logging(app)
- app.logger.info("Application initialized in %s mode", env)
- except Exception:
- # keep app creation robust even if logging setup fails
- pass
- register_blueprints(app)
- # Register a global handler to convert APIException instances into JSON responses
- try:
- from app.exceptions.api_exception import APIException
- @app.errorhandler(APIException)
- def handle_api_exception(error):
- # HTTPException.get_response builds a Response object using get_body/get_headers
- response = error.get_response()
- return response
- except Exception:
- pass
- return app
|