🎉 Improved backend structure & cleaned up files (#123) - Added start scripts for development & production environments. 🛠️ Removed unnecessary database files. 📚 Refactored logging system for better performance & organization. 💄 Fixed minor typo in app.log file.

This commit is contained in:
2025-06-11 10:26:39 +02:00
parent 50d4c62725
commit 24cd99a1f6
18 changed files with 274 additions and 83 deletions

View File

@@ -19,46 +19,26 @@ from sqlalchemy import event
from contextlib import contextmanager
import threading
# ===== OPTIMIERTE KONFIGURATION FÜR RASPBERRY PI =====
class OptimizedConfig:
"""Konfiguration für performance-optimierte Bereitstellung auf Raspberry Pi"""
# Performance-Optimierungs-Flags
OPTIMIZED_MODE = True
USE_MINIFIED_ASSETS = True
DISABLE_ANIMATIONS = True
LIMIT_GLASSMORPHISM = True
# Flask-Performance-Einstellungen
DEBUG = False
TESTING = False
SEND_FILE_MAX_AGE_DEFAULT = 31536000 # 1 Jahr Cache für statische Dateien
# Template-Einstellungen
TEMPLATES_AUTO_RELOAD = False
EXPLAIN_TEMPLATE_LOADING = False
# Session-Konfiguration
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Lax'
# Performance-Optimierungen
MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB max Upload
JSON_SORT_KEYS = False
JSONIFY_PRETTYPRINT_REGULAR = False
# ===== PRODUCTION-KONFIGURATION =====
class ProductionConfig:
"""Production-Konfiguration für Mercedes-Benz TBA Marienfelde Air-Gapped Environment"""
"""Production-Konfiguration für Mercedes-Benz TBA Marienfelde Air-Gapped Environment
Enthält alle Performance-Optimierungen, die vorher in OptimizedConfig waren,
plus Production-spezifische Sicherheits- und Compliance-Einstellungen.
"""
# Umgebung
ENV = 'production'
DEBUG = False
TESTING = False
# Sicherheit
SECRET_KEY = os.environ.get('SECRET_KEY') or SECRET_KEY
# Performance-Optimierungen (ehemals OptimizedConfig)
OPTIMIZED_MODE = True
USE_MINIFIED_ASSETS = True
DISABLE_ANIMATIONS = True
LIMIT_GLASSMORPHISM = True
# Sicherheit (SECRET_KEY wird später gesetzt)
WTF_CSRF_ENABLED = True
WTF_CSRF_TIME_LIMIT = 3600 # 1 Stunde
@@ -66,10 +46,10 @@ class ProductionConfig:
SESSION_COOKIE_SECURE = True # HTTPS erforderlich
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Strict'
PERMANENT_SESSION_LIFETIME = SESSION_LIFETIME
# PERMANENT_SESSION_LIFETIME wird später gesetzt
# Performance-Optimierungen
SEND_FILE_MAX_AGE_DEFAULT = 2592000 # 30 Tage Cache
SEND_FILE_MAX_AGE_DEFAULT = 31536000 # 1 Jahr Cache für statische Dateien
TEMPLATES_AUTO_RELOAD = False
EXPLAIN_TEMPLATE_LOADING = False
@@ -120,6 +100,76 @@ class ProductionConfig:
ENABLE_HEALTH_CHECKS = True
ENABLE_PERFORMANCE_MONITORING = True
# ===== DEVELOPMENT-KONFIGURATION =====
class DevelopmentConfig:
"""Development-Konfiguration für lokale Entwicklung
Konsolidiert alle Nicht-Production-Modi (development, default, fallback).
Optimiert für Entwicklerfreundlichkeit und Debugging.
"""
# Umgebung
ENV = 'development'
DEBUG = True
TESTING = False
# Performance (moderat optimiert für bessere Entwicklererfahrung)
OPTIMIZED_MODE = False
USE_MINIFIED_ASSETS = False
DISABLE_ANIMATIONS = False
LIMIT_GLASSMORPHISM = False
# Sicherheit (relaxed für Development)
WTF_CSRF_ENABLED = True
WTF_CSRF_TIME_LIMIT = 7200 # 2 Stunden für längere Dev-Sessions
# Session-Sicherheit (relaxed)
SESSION_COOKIE_SECURE = False # HTTP OK für Development
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Lax'
# Performance (Developer-freundlich)
SEND_FILE_MAX_AGE_DEFAULT = 1 # Keine Cache für Development
TEMPLATES_AUTO_RELOAD = True
EXPLAIN_TEMPLATE_LOADING = True
# Upload-Beschränkungen (generous für Testing)
MAX_CONTENT_LENGTH = 50 * 1024 * 1024 # 50MB für Development
# JSON (Pretty für Debugging)
JSON_SORT_KEYS = True
JSONIFY_PRETTYPRINT_REGULAR = True
JSONIFY_MIMETYPE = 'application/json'
# Logging-Level
LOG_LEVEL = 'DEBUG'
# Entwicklungs-Einstellungen
OFFLINE_MODE = False
DISABLE_EXTERNAL_APIS = False
USE_LOCAL_ASSETS_ONLY = False
# Datenbank (Developer-freundlich)
SQLALCHEMY_TRACK_MODIFICATIONS = True # Für Debugging
SQLALCHEMY_POOL_RECYCLE = 1800 # 30 Minuten
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_ENGINE_OPTIONS = {
'pool_pre_ping': True,
'pool_recycle': 1800,
'echo': True # SQL-Logging für Development
}
# Development-spezifische Einstellungen
COMPANY_NAME = "MYP Development Environment"
ENVIRONMENT_NAME = "Development/Testing"
COMPLIANCE_MODE = False
AUDIT_LOGGING = False
# Monitoring (minimal für Development)
ENABLE_METRICS = False
ENABLE_HEALTH_CHECKS = False
ENABLE_PERFORMANCE_MONITORING = False
def detect_raspberry_pi():
"""Erkennt ob das System auf einem Raspberry Pi läuft"""
try:
@@ -172,40 +222,27 @@ def detect_production_environment():
except:
pass
return False
def get_environment_type():
"""Bestimmt den Umgebungstyp"""
if detect_production_environment():
return 'production'
elif should_use_optimized_config():
return 'optimized'
elif os.getenv('FLASK_ENV', '').lower() in ['development', 'dev']:
return 'development'
else:
return 'default'
def should_use_optimized_config():
"""Bestimmt ob die optimierte Konfiguration verwendet werden soll"""
if '--optimized' in sys.argv:
return True
# Automatische Production-Erkennung für Raspberry Pi oder Low-Memory-Systeme
if detect_raspberry_pi():
return True
if os.getenv('USE_OPTIMIZED_CONFIG', '').lower() in ['true', '1', 'yes']:
return True
try:
import psutil
memory_gb = psutil.virtual_memory().total / (1024**3)
if memory_gb < 2.0:
if memory_gb < 2.0: # Unter 2GB RAM = wahrscheinlich Production-Umgebung
return True
except:
pass
return False
def get_environment_type():
"""Bestimmt den Umgebungstyp - nur noch production oder development"""
if detect_production_environment():
return 'production'
else:
return 'development'
# Windows-spezifische Fixes
if os.name == 'nt':
try:
@@ -224,9 +261,6 @@ from utils.job_scheduler import JobScheduler, get_job_scheduler
from utils.queue_manager import start_queue_manager, stop_queue_manager
from utils.settings import SECRET_KEY, SESSION_LIFETIME
# ===== OFFLINE-MODUS KONFIGURATION =====
OFFLINE_MODE = True # Produktionseinstellung für Offline-Betrieb
# Blueprints importieren
from blueprints.auth import auth_blueprint
# from blueprints.user import user_blueprint # Konsolidiert in user_management
@@ -340,33 +374,21 @@ app.secret_key = SECRET_KEY
# ===== KONFIGURATION ANWENDEN =====
ENVIRONMENT_TYPE = get_environment_type()
USE_PRODUCTION_CONFIG = detect_production_environment()
USE_OPTIMIZED_CONFIG = should_use_optimized_config()
app_logger.info(f"[CONFIG] Erkannte Umgebung: {ENVIRONMENT_TYPE}")
app_logger.info(f"[CONFIG] Production-Modus: {USE_PRODUCTION_CONFIG}")
app_logger.info(f"[CONFIG] Optimiert-Modus: {USE_OPTIMIZED_CONFIG}")
if USE_PRODUCTION_CONFIG:
apply_production_config(app)
elif USE_OPTIMIZED_CONFIG:
apply_optimized_config(app)
else:
# Standard-Entwicklungskonfiguration
app_logger.info("[CONFIG] Verwende Standard-Entwicklungskonfiguration")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.jinja_env.globals.update({
'optimized_mode': False,
'production_mode': False,
'use_minified_assets': False,
'disable_animations': False,
'limit_glassmorphism': False,
'base_template': 'base.html'
})
# Development-Konfiguration (konsolidiert default/fallback)
app_logger.info("[CONFIG] Verwende Development-Konfiguration")
apply_development_config(app)
# Umgebungs-spezifische Einstellungen
if OFFLINE_MODE or getattr(ProductionConfig, 'OFFLINE_MODE', False):
OFFLINE_MODE = getattr(ProductionConfig, 'OFFLINE_MODE', False) if USE_PRODUCTION_CONFIG else getattr(DevelopmentConfig, 'OFFLINE_MODE', False)
if OFFLINE_MODE:
app_logger.info("[CONFIG] ✅ Air-Gapped/Offline-Modus aktiviert")
app.config['DISABLE_EXTERNAL_REQUESTS'] = True
@@ -404,7 +426,6 @@ def load_user(user_id):
# ===== BLUEPRINTS REGISTRIEREN =====
app.register_blueprint(auth_blueprint)
# app.register_blueprint(user_blueprint) # Konsolidiert in users_blueprint
# Vereinheitlichte Admin-Blueprints registrieren
app.register_blueprint(admin_blueprint)
app.register_blueprint(admin_api_blueprint)
@@ -444,7 +465,7 @@ def format_datetime_filter(value, format='%d.%m.%Y %H:%M'):
@app.template_global()
def is_optimized_mode():
"""Prüft ob der optimierte Modus aktiv ist"""
return USE_OPTIMIZED_CONFIG
return USE_PRODUCTION_CONFIG
# ===== REQUEST HOOKS =====
@app.before_request