from flask import Flask, jsonify import os from flask_swagger_ui import get_swaggerui_blueprint from flask_cors import CORS from app.config import Config from app.api.routes import api, auth_bp from app.errors import register_error_handlers from flask_jwt_extended import JWTManager def create_app(): """ Factory function to create a Flask app instance """ app = Flask(__name__) # Load configuration app.config.from_object(Config) jwt = JWTManager(app) CORS(app) # Swagger Doc SWAGGER_URL = app.config.get("SWAGGER_URL") API_URL = app.config.get("API_URL") # Register blueprints with /api prefix for the main API routes app.register_blueprint(api, url_prefix='/api') # Register blueprints with /auth prefix for the authentication routes app.register_blueprint(auth_bp, url_prefix='/api/Auth') swagger_ui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL) app.register_blueprint(swagger_ui_blueprint, url_prefix=SWAGGER_URL) def jwt_error(message, code=401): return jsonify({ "status": "error", "message": message }), code @jwt.unauthorized_loader def unauthorized_response(callback): return jwt_error("Unauthorized access") @jwt.expired_token_loader def expired_token_callback(jwt_header, jwt_payload): return jwt_error("Expired token") @jwt.invalid_token_loader def invalid_token_callback(error): return jwt_error("Invalid authentication token.", 422) @jwt.revoked_token_loader def revoked_token_callback(jwt_header, jwt_payload): return jwt_error("This token has been revoked. Please log in again.") # Error Handlers register_error_handlers(app) return app