2026-02-25 09:29:45 +00:00
|
|
|
import json
|
2026-02-24 16:19:15 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
2026-02-14 16:56:10 +00:00
|
|
|
from flask import Flask, jsonify
|
|
|
|
|
from flask_cors import CORS
|
|
|
|
|
from flask_jwt_extended import JWTManager
|
|
|
|
|
from flask_migrate import Migrate
|
2026-02-24 16:19:15 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Create extensions but don't initialize them yet
|
|
|
|
|
db = SQLAlchemy()
|
|
|
|
|
migrate = Migrate()
|
|
|
|
|
jwt = JWTManager()
|
|
|
|
|
cors = CORS()
|
2026-02-21 18:38:19 +00:00
|
|
|
load_dotenv(override=True)
|
2026-02-14 16:56:10 +00:00
|
|
|
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
def create_app(config_name=None):
|
|
|
|
|
"""Application factory pattern"""
|
|
|
|
|
app = Flask(__name__)
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Load configuration
|
|
|
|
|
if config_name is None:
|
2026-02-21 18:38:19 +00:00
|
|
|
config_name = os.environ.get("FLASK_ENV", "development")
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
from app.config import config_by_name
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
app.config.from_object(config_by_name[config_name])
|
2026-02-24 16:19:15 +00:00
|
|
|
|
|
|
|
|
print("----------------------------------------------------------")
|
|
|
|
|
print(
|
|
|
|
|
f"------------------ENVIRONMENT: {config_name}-------------------------------------"
|
|
|
|
|
)
|
2026-02-21 18:38:19 +00:00
|
|
|
# print(F'------------------CONFIG: {app.config}-------------------------------------')
|
2026-02-25 09:29:45 +00:00
|
|
|
print(json.dumps(dict(app.config), indent=2, default=str))
|
2026-02-24 16:19:15 +00:00
|
|
|
print("----------------------------------------------------------")
|
2026-02-14 16:56:10 +00:00
|
|
|
# Initialize extensions with app
|
|
|
|
|
db.init_app(app)
|
|
|
|
|
migrate.init_app(app, db)
|
|
|
|
|
jwt.init_app(app)
|
2026-02-24 16:19:15 +00:00
|
|
|
cors.init_app(
|
|
|
|
|
app, resources={r"/api/*": {"origins": app.config.get("CORS_ORIGINS", "*")}}
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
# Initialize Celery
|
|
|
|
|
from app.celery import init_celery
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
init_celery(app)
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Import models (required for migrations)
|
2026-02-25 16:48:18 +00:00
|
|
|
from app.models import (
|
|
|
|
|
board,
|
|
|
|
|
card,
|
|
|
|
|
card_label,
|
|
|
|
|
check_item,
|
|
|
|
|
checklist,
|
|
|
|
|
comment,
|
|
|
|
|
label,
|
|
|
|
|
list_model,
|
|
|
|
|
order,
|
|
|
|
|
product,
|
|
|
|
|
user,
|
|
|
|
|
) # noqa: F401
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Register blueprints
|
|
|
|
|
from app.routes import api_bp, health_bp
|
2026-02-25 16:48:18 +00:00
|
|
|
from app.routes.kanban import kanban_bp
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
app.register_blueprint(api_bp, url_prefix="/api")
|
2026-02-14 16:56:10 +00:00
|
|
|
app.register_blueprint(health_bp)
|
2026-02-25 16:48:18 +00:00
|
|
|
app.register_blueprint(kanban_bp, url_prefix="/api")
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Global error handlers
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
|
def not_found(error):
|
2026-02-24 14:36:31 +00:00
|
|
|
print(f"404 Error: {error}")
|
2026-02-21 18:38:19 +00:00
|
|
|
return jsonify({"error": "Not found"}), 404
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
@app.errorhandler(500)
|
|
|
|
|
def internal_error(error):
|
2026-02-24 14:36:31 +00:00
|
|
|
print(f"500 Error: {error}")
|
2026-02-21 18:38:19 +00:00
|
|
|
return jsonify({"error": "Internal server error"}), 500
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-24 14:36:31 +00:00
|
|
|
@app.errorhandler(422)
|
|
|
|
|
def validation_error(error):
|
|
|
|
|
print(f"422 Error: {error}")
|
|
|
|
|
return jsonify({"error": "Validation error"}), 422
|
2026-02-24 16:19:15 +00:00
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
return app
|