2026-03-15 15:24:34 +00:00
|
|
|
# import json
|
2026-02-24 16:19:15 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
2026-08-09 12:49:45 +00:00
|
|
|
from flask import Flask, jsonify, request
|
2026-02-14 16:56:10 +00:00
|
|
|
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
|
|
|
|
2026-03-15 15:24:34 +00:00
|
|
|
# print("----------------------------------------------------------")
|
|
|
|
|
# print(f"------------------ENVIRONMENT: {config_name}-----------------------")
|
|
|
|
|
# print(json.dumps(dict(app.config), indent=2, default=str))
|
|
|
|
|
# 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-03-20 17:17:01 +00:00
|
|
|
# Import storage extension
|
|
|
|
|
from app.services.storage.storage_extension import storage
|
|
|
|
|
|
|
|
|
|
if not (app.config.get("TESTING") or app.config.get("USE_MOCK_STORAGE")):
|
|
|
|
|
# Initialize storage extension (MinIO)
|
|
|
|
|
storage.init_app(app)
|
|
|
|
|
|
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-24 16:19:15 +00:00
|
|
|
|
2026-02-14 16:56:10 +00:00
|
|
|
# Register blueprints
|
2026-03-15 12:52:44 +00:00
|
|
|
from app.routes import api_bp, health_bp, home_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-03-15 12:52:44 +00:00
|
|
|
app.register_blueprint(health_bp, url_prefix="/health")
|
|
|
|
|
app.register_blueprint(home_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-08-09 12:49:45 +00:00
|
|
|
# Reject requests whose Host header is not in ALLOWED_HOSTS. This blocks
|
|
|
|
|
# direct IP:port access and unknown-domain access at the application level
|
|
|
|
|
# (defense-in-depth behind Traefik). The /health endpoint is exempt so
|
|
|
|
|
# Docker/Traefik health checks (Host: localhost:8000) keep working even
|
|
|
|
|
# when ALLOWED_HOSTS is set.
|
|
|
|
|
@app.before_request
|
|
|
|
|
def reject_disallowed_hosts():
|
|
|
|
|
allowed_hosts = app.config.get("ALLOWED_HOSTS") or []
|
|
|
|
|
if not allowed_hosts:
|
|
|
|
|
return None
|
|
|
|
|
if request.path.startswith("/health"):
|
|
|
|
|
return None
|
|
|
|
|
host = (request.host or "").split(":")[0].lower()
|
|
|
|
|
if host and host not in allowed_hosts:
|
|
|
|
|
return jsonify({"error": "Not found"}), 404
|
|
|
|
|
return None
|
|
|
|
|
|
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-25 18:32:57 +00:00
|
|
|
@app.teardown_appcontext
|
|
|
|
|
def shutdown_session(exception=None):
|
|
|
|
|
"""Remove session at end of request to return connection to pool"""
|
|
|
|
|
db.session.remove()
|
|
|
|
|
|
2026-02-21 18:38:19 +00:00
|
|
|
return app
|