app.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Application factory."""
  2. from flask import Flask
  3. from app.config.setting import config
  4. def register_blueprints(app):
  5. """Register all application blueprints."""
  6. from app.api.v1 import create_blueprint_v1
  7. from app.routes.health import bp as health_bp
  8. # API v1 under /v1
  9. app.register_blueprint(create_blueprint_v1(), url_prefix="/v1")
  10. # Health endpoint at /health
  11. app.register_blueprint(health_bp)
  12. def create_app(env: str = "default"):
  13. """Create and configure the Flask application."""
  14. app = Flask(__name__)
  15. app.config.from_object(config[env])
  16. # optional: initialize logging and other extensions here
  17. try:
  18. from app.utils.logger import setup_logging
  19. setup_logging(app)
  20. except Exception:
  21. # keep app creation robust even if logging setup fails
  22. pass
  23. register_blueprints(app)
  24. # Register a global handler to convert APIException instances into JSON responses
  25. try:
  26. from app.exceptions.api_exception import APIException
  27. @app.errorhandler(APIException)
  28. def handle_api_exception(error):
  29. # HTTPException.get_response builds a Response object using get_body/get_headers
  30. response = error.get_response()
  31. return response
  32. except Exception:
  33. pass
  34. return app