"Refactor backend configuration and setup for improved maintainability (feat)"
This commit is contained in:
parent
a11721f677
commit
9614267d48
@ -1952,19 +1952,7 @@ else:
|
|||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
init_db()
|
init_db()
|
||||||
printers_config = json.loads(app.config.get('PRINTERS', '{}'))
|
printers_config = app.config.get('PRINTERS', {})
|
||||||
if printers_config:
|
if printers_config:
|
||||||
init_printers()
|
init_printers()
|
||||||
setup_frontend_v2()
|
setup_frontend_v2()
|
||||||
|
|
||||||
# Parse PRINTERS als JSON
|
|
||||||
printers_env = app.config.get('PRINTERS', '{}')
|
|
||||||
if isinstance(printers_env, str):
|
|
||||||
try:
|
|
||||||
app.config['PRINTERS'] = json.loads(printers_env)
|
|
||||||
except (json.JSONDecodeError, TypeError):
|
|
||||||
app.config['PRINTERS'] = {}
|
|
||||||
elif isinstance(printers_env, dict):
|
|
||||||
app.config['PRINTERS'] = printers_env
|
|
||||||
else:
|
|
||||||
app.config['PRINTERS'] = {}
|
|
@ -125,6 +125,11 @@ class ProductionConfig(Config):
|
|||||||
if not os.path.exists('logs'):
|
if not os.path.exists('logs'):
|
||||||
os.mkdir('logs')
|
os.mkdir('logs')
|
||||||
|
|
||||||
|
# Prüfe ob Datei-Logging deaktiviert ist (für Tests)
|
||||||
|
if app.config.get('DISABLE_FILE_LOGGING', False):
|
||||||
|
app.logger.info('Datei-Logging deaktiviert (Test-Modus)')
|
||||||
|
return
|
||||||
|
|
||||||
# Windows-kompatibles Logging
|
# Windows-kompatibles Logging
|
||||||
import platform
|
import platform
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
@ -205,30 +210,21 @@ class ProductionConfig(Config):
|
|||||||
app.logger.warning('Security module not found, skipping security middleware')
|
app.logger.warning('Security module not found, skipping security middleware')
|
||||||
|
|
||||||
class TestingConfig(Config):
|
class TestingConfig(Config):
|
||||||
"""Konfiguration für die Testumgebung."""
|
"""Konfiguration für Tests"""
|
||||||
|
|
||||||
DEBUG = True
|
|
||||||
TESTING = True
|
TESTING = True
|
||||||
|
DEBUG = True
|
||||||
# In-Memory-Datenbank für Tests
|
|
||||||
DATABASE = ':memory:'
|
|
||||||
|
|
||||||
# Deaktiviere CSRF für Tests
|
|
||||||
WTF_CSRF_ENABLED = False
|
WTF_CSRF_ENABLED = False
|
||||||
|
DATABASE_PATH = ':memory:' # In-Memory-Datenbank für Tests
|
||||||
|
|
||||||
# Kürzere Session-Lebensdauer für Tests
|
# Deaktiviere Datei-Logging für Tests (Windows-Kompatibilität)
|
||||||
PERMANENT_SESSION_LIFETIME = timedelta(minutes=5)
|
DISABLE_FILE_LOGGING = True
|
||||||
|
|
||||||
# Kürzere Job-Check-Intervalle für Tests
|
|
||||||
JOB_CHECK_INTERVAL = 5
|
|
||||||
|
|
||||||
# Deaktiviere Sicherheit für Tests
|
|
||||||
SECURITY_ENABLED = False
|
|
||||||
RATE_LIMIT_ENABLED = False
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_app(app):
|
def init_app(app):
|
||||||
Config.init_app(app)
|
"""Initialisierung für Test-Umgebung"""
|
||||||
|
# Nur Console-Logging für Tests
|
||||||
|
import logging
|
||||||
|
app.logger.setLevel(logging.WARNING) # Reduziere Log-Level für Tests
|
||||||
|
|
||||||
# Konfigurationsmapping
|
# Konfigurationsmapping
|
||||||
config = {
|
config = {
|
||||||
|
@ -199,28 +199,35 @@ def test_health_endpoint():
|
|||||||
print("🏥 Teste Health-Check...")
|
print("🏥 Teste Health-Check...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.environ['SECRET_KEY'] = 'test_secret_key'
|
# Verwende Test-Konfiguration um Logging-Probleme zu vermeiden
|
||||||
os.environ['DATABASE_PATH'] = ':memory:'
|
|
||||||
|
|
||||||
from app import create_app
|
from app import create_app
|
||||||
|
|
||||||
app = create_app('testing')
|
app = create_app('testing')
|
||||||
|
|
||||||
with app.test_client() as client:
|
with app.test_client() as client:
|
||||||
response = client.get('/health')
|
# Teste sowohl /health als auch /monitoring/health
|
||||||
|
endpoints_to_test = ['/health', '/monitoring/health']
|
||||||
|
|
||||||
if response.status_code == 200:
|
for endpoint in endpoints_to_test:
|
||||||
data = response.get_json()
|
response = client.get(endpoint)
|
||||||
if data and data.get('status') == 'healthy':
|
|
||||||
print(" ✅ Health-Check funktioniert")
|
if response.status_code == 200:
|
||||||
print(f" ✅ Service: {data.get('service')}")
|
data = response.get_json()
|
||||||
return True
|
if data and (data.get('status') == 'healthy' or data.get('status') == 'ok'):
|
||||||
|
print(f" ✅ Health-Check funktioniert ({endpoint})")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print(f" ❌ Health-Check-Response ungültig ({endpoint}): {data}")
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
print(f" ❌ Health-Check-Antwort fehlerhaft: {data}")
|
print(f" ❌ Health-Check fehlgeschlagen ({endpoint}): {response.status_code}")
|
||||||
return False
|
continue
|
||||||
else:
|
|
||||||
print(f" ❌ Health-Check fehlgeschlagen: {response.status_code}")
|
# Wenn alle Endpoints fehlschlagen, zeige verfügbare Routen
|
||||||
return False
|
print(" 📋 Verfügbare Health-Routen:")
|
||||||
|
for rule in app.url_map.iter_rules():
|
||||||
|
if 'health' in rule.rule.lower():
|
||||||
|
print(f" - {rule.rule} [{', '.join(rule.methods)}]")
|
||||||
|
return False
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" ❌ Health-Check-Fehler: {e}")
|
print(f" ❌ Health-Check-Fehler: {e}")
|
||||||
|
@ -1,37 +1,44 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
WSGI-Einstiegspunkt für die MYP Flask-Anwendung.
|
WSGI-Konfiguration für MYP Backend
|
||||||
Verwendet für den Produktionsbetrieb mit WSGI-Servern wie Gunicorn.
|
Lädt die Flask-Anwendung für Produktions-Deployment mit Gunicorn
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
# Lade Umgebungsvariablen
|
# Füge das Backend-Verzeichnis zum Python-Pfad hinzu
|
||||||
load_dotenv()
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
from app import create_app
|
from app import create_app, init_db, init_printers, setup_frontend_v2
|
||||||
|
|
||||||
# Erstelle Anwendungsinstanz für Produktionsbetrieb
|
# Bestimme die Umgebung
|
||||||
flask_env = os.environ.get('FLASK_ENV', 'production')
|
flask_env = os.environ.get('FLASK_ENV', 'production')
|
||||||
|
|
||||||
|
# Erstelle die Flask-Anwendung
|
||||||
application = create_app(flask_env)
|
application = create_app(flask_env)
|
||||||
|
|
||||||
# Initialisierung für WSGI-Server
|
# Initialisierung für WSGI-Server
|
||||||
with application.app_context():
|
with application.app_context():
|
||||||
from app import init_db, init_printers, setup_frontend_v2
|
|
||||||
import json
|
|
||||||
|
|
||||||
# Datenbank initialisieren
|
# Datenbank initialisieren
|
||||||
init_db()
|
init_db()
|
||||||
|
|
||||||
# Drucker initialisieren, falls konfiguriert
|
# Drucker initialisieren (falls konfiguriert)
|
||||||
printers_config = json.loads(application.config.get('PRINTERS', '{}'))
|
printers_config = application.config.get('PRINTERS', {})
|
||||||
if printers_config:
|
if printers_config:
|
||||||
init_printers()
|
init_printers()
|
||||||
|
|
||||||
# Frontend v2 Setup
|
# Frontend V2 einrichten
|
||||||
setup_frontend_v2()
|
setup_frontend_v2()
|
||||||
|
|
||||||
application.logger.info(f'MYP Backend gestartet in {flask_env} Modus')
|
# Logging für WSGI-Start
|
||||||
|
if hasattr(application, 'logger'):
|
||||||
|
application.logger.info(f'MYP Backend WSGI application started in {flask_env} mode')
|
||||||
|
|
||||||
|
# Export für Gunicorn
|
||||||
|
app = application
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
application.run()
|
# Für direkte Ausführung (Debug)
|
||||||
|
application.run(host='0.0.0.0', port=5000, debug=(flask_env == 'development'))
|
Loading…
x
Reference in New Issue
Block a user