Now fabric function creates app
This commit is contained in:
		
							
								
								
									
										78
									
								
								api/app.py
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								api/app.py
									
									
									
									
									
								
							| @@ -4,44 +4,56 @@ from flask_jwt_extended import JWTManager | |||||||
| from jwt import ExpiredSignatureError | from jwt import ExpiredSignatureError | ||||||
| from models import db | from models import db | ||||||
| import os | import os | ||||||
| from utils import init_db | from task_views import task_bp | ||||||
| from views import user_bp | from user_views import user_bp, init_db | ||||||
| from werkzeug.exceptions import HTTPException | from werkzeug.exceptions import HTTPException | ||||||
|  |  | ||||||
| # App initialization | def create_app(config_name="default"): | ||||||
| load_dotenv() |     """Creates and returns a new instance of Flask app.""" | ||||||
| app = Flask(__name__) |     load_dotenv() | ||||||
| app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('SQLALCHEMY_DATABASE_URI') |     app = Flask(__name__) | ||||||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True |  | ||||||
| app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET_KEY', 'changeme') |  | ||||||
|      |      | ||||||
| # Blueprint registration |     # Database settings | ||||||
| app.register_blueprint(user_bp) |     if config_name == "testing": | ||||||
|  |         app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"  # Database in memory | ||||||
|  |         app.config["TESTING"] = True | ||||||
|  |     else: | ||||||
|  |         app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("SQLALCHEMY_DATABASE_URI") | ||||||
|      |      | ||||||
| # Database and JWT initialization |     app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||||||
| db.init_app(app) |     app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY", "changeme") | ||||||
| jwt = JWTManager(app) |  | ||||||
|  |  | ||||||
| # Global error handler |     # Blueprints registration | ||||||
| @app.errorhandler(Exception) |     app.register_blueprint(user_bp) | ||||||
| def global_error_handler(error): |     app.register_blueprint(task_bp) | ||||||
|     if isinstance(error, HTTPException): |  | ||||||
|         response = jsonify({"error": error.description}) |     # Database and JWT initialization | ||||||
|         response.status_code = error.code |     db.init_app(app) | ||||||
|     elif isinstance(error, ExpiredSignatureError): |     jwt = JWTManager(app) | ||||||
|         response = jsonify({"error": "Token has expired"}) |  | ||||||
|         response.status_code = 401 |     # Global error handler | ||||||
|     else:  # Wszystkie inne błędy |     @app.errorhandler(Exception) | ||||||
|         response = jsonify({"error": str(error)}) |     def global_error_handler(error): | ||||||
|         response.status_code = 500 |         if isinstance(error, HTTPException): | ||||||
|     return response |             response = jsonify({"error": error.description}) | ||||||
|  |             response.status_code = error.code | ||||||
|  |         elif isinstance(error, ExpiredSignatureError): | ||||||
|  |             response = jsonify({"error": "Token has expired"}) | ||||||
|  |             response.status_code = 401 | ||||||
|  |         else:  # All other errors | ||||||
|  |             response = jsonify({"error": str(error)}) | ||||||
|  |             response.status_code = 500 | ||||||
|  |         return response | ||||||
|  |  | ||||||
|  |     # Fill database by initial values (only if we are not testing) | ||||||
|  |     with app.app_context(): | ||||||
|  |         db.create_all() | ||||||
|  |         if config_name != "testing": | ||||||
|  |             init_db() | ||||||
|  |     return app | ||||||
|  |  | ||||||
|  |  | ||||||
| # Fill database by initial values | # Server start only if we run app directly | ||||||
| with app.app_context(): |  | ||||||
|     db.create_all() |  | ||||||
|     init_db() |  | ||||||
|  |  | ||||||
| # Server start |  | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     app.run(host='0.0.0.0') |     app = create_app() | ||||||
|  |     app.run(host="0.0.0.0") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user