Now fabric function creates app
This commit is contained in:
		
							
								
								
									
										36
									
								
								api/app.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								api/app.py
									
									
									
									
									
								
							| @@ -4,19 +4,28 @@ 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"): | ||||||
|  |     """Creates and returns a new instance of Flask app.""" | ||||||
|     load_dotenv() |     load_dotenv() | ||||||
|     app = Flask(__name__) |     app = Flask(__name__) | ||||||
| app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('SQLALCHEMY_DATABASE_URI') |  | ||||||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True |  | ||||||
| app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET_KEY', 'changeme') |  | ||||||
|      |      | ||||||
| # Blueprint registration |     # Database settings | ||||||
|  |     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") | ||||||
|  |      | ||||||
|  |     app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | ||||||
|  |     app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY", "changeme") | ||||||
|  |  | ||||||
|  |     # Blueprints registration | ||||||
|     app.register_blueprint(user_bp) |     app.register_blueprint(user_bp) | ||||||
|  |     app.register_blueprint(task_bp) | ||||||
|  |  | ||||||
|     # Database and JWT initialization |     # Database and JWT initialization | ||||||
|     db.init_app(app) |     db.init_app(app) | ||||||
| @@ -31,17 +40,20 @@ def global_error_handler(error): | |||||||
|         elif isinstance(error, ExpiredSignatureError): |         elif isinstance(error, ExpiredSignatureError): | ||||||
|             response = jsonify({"error": "Token has expired"}) |             response = jsonify({"error": "Token has expired"}) | ||||||
|             response.status_code = 401 |             response.status_code = 401 | ||||||
|     else:  # Wszystkie inne błędy |         else:  # All other errors | ||||||
|             response = jsonify({"error": str(error)}) |             response = jsonify({"error": str(error)}) | ||||||
|             response.status_code = 500 |             response.status_code = 500 | ||||||
|         return response |         return response | ||||||
|  |  | ||||||
|  |     # Fill database by initial values (only if we are not testing) | ||||||
| # Fill database by initial values |  | ||||||
|     with app.app_context(): |     with app.app_context(): | ||||||
|         db.create_all() |         db.create_all() | ||||||
|  |         if config_name != "testing": | ||||||
|             init_db() |             init_db() | ||||||
|  |     return app | ||||||
|  |  | ||||||
| # Server start |  | ||||||
|  | # Server start only if we run app directly | ||||||
| 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