"Refactor database connection and settings for improved performance (feat)"

This commit is contained in:
Till Tomczak 2025-05-26 10:07:37 +02:00
parent 9efe122e14
commit e747ac945c
4 changed files with 55 additions and 18 deletions

View File

@ -1395,10 +1395,13 @@ def check_jobs():
# App starten # App starten
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
import threading
# Kommandozeilenargumente parsen # Kommandozeilenargumente parsen
parser = argparse.ArgumentParser(description='MYP Platform - 3D-Drucker Reservierungssystem') parser = argparse.ArgumentParser(description='MYP Platform - 3D-Drucker Reservierungssystem')
parser.add_argument('--port', type=int, help='Port für den Server (überschreibt die Konfiguration)') parser.add_argument('--port', type=int, help='Port für den Server (überschreibt die Konfiguration)')
parser.add_argument('--no-ssl', action='store_true', help='Deaktiviert SSL/HTTPS')
parser.add_argument('--dual-protocol', action='store_true', help='Startet sowohl HTTP als auch HTTPS Server')
args = parser.parse_args() args = parser.parse_args()
# Initialisierung # Initialisierung
@ -1407,25 +1410,58 @@ if __name__ == "__main__":
# Port aus Kommandozeilenargument verwenden, falls angegeben # Port aus Kommandozeilenargument verwenden, falls angegeben
port = args.port if args.port else FLASK_PORT port = args.port if args.port else FLASK_PORT
# Server starten # SSL-Kontext abrufen
app_logger.info(f"Server wird auf Port {port} gestartet...") ssl_context = None if args.no_ssl else get_ssl_context()
# Funktion zum Starten des Servers
def start_server(use_ssl=True, server_port=None):
if server_port is None:
server_port = port
try: try:
ssl_context = get_ssl_context() if use_ssl and ssl_context:
if ssl_context: protocol = "HTTPS"
app.run(host=FLASK_HOST, port=port, debug=FLASK_DEBUG, ssl_context=ssl_context) app_logger.info(f"{protocol}-Server wird auf Port {server_port} gestartet...")
app.run(host=FLASK_HOST, port=server_port, debug=FLASK_DEBUG, ssl_context=ssl_context)
else: else:
app.run(host=FLASK_HOST, port=port, debug=FLASK_DEBUG) protocol = "HTTP"
app_logger.info(f"{protocol}-Server wird auf Port {server_port} gestartet...")
app.run(host=FLASK_HOST, port=server_port, debug=FLASK_DEBUG)
except Exception as e: except Exception as e:
app_logger.error(f"Fehler beim Starten des Servers: {str(e)}") app_logger.error(f"Fehler beim Starten des {protocol}-Servers auf Port {server_port}: {str(e)}")
app_logger.info(f"Versuche auf Fallback-Port {FLASK_FALLBACK_PORT} zu starten...") if server_port == FLASK_PORT:
fallback_port = FLASK_FALLBACK_PORT
app_logger.info(f"Versuche auf Fallback-Port {fallback_port} zu starten...")
try: try:
app.run(host=FLASK_HOST, port=FLASK_FALLBACK_PORT, debug=FLASK_DEBUG) if use_ssl and ssl_context:
app.run(host=FLASK_HOST, port=fallback_port, debug=FLASK_DEBUG, ssl_context=ssl_context)
else:
app.run(host=FLASK_HOST, port=fallback_port, debug=FLASK_DEBUG)
except Exception as e2: except Exception as e2:
app_logger.error(f"Auch Fallback-Port fehlgeschlagen: {str(e2)}") app_logger.error(f"Auch Fallback-Port fehlgeschlagen: {str(e2)}")
app_logger.info("Versuche auf Standard-Port 5000 zu starten...") app_logger.info("Versuche auf Standard-Port 5000 zu starten...")
if use_ssl and ssl_context:
app.run(host=FLASK_HOST, port=5000, debug=FLASK_DEBUG, ssl_context=ssl_context)
else:
app.run(host=FLASK_HOST, port=5000, debug=FLASK_DEBUG) app.run(host=FLASK_HOST, port=5000, debug=FLASK_DEBUG)
# Dual-Protokoll-Modus: HTTP und HTTPS parallel starten
if args.dual_protocol:
app_logger.info("Starte Server im Dual-Protokoll-Modus (HTTP und HTTPS)...")
# HTTPS auf Hauptport (443)
https_thread = threading.Thread(target=start_server, kwargs={"use_ssl": True, "server_port": port})
https_thread.daemon = True
https_thread.start()
# HTTP auf Alternativport (80)
http_port = FLASK_FALLBACK_PORT
start_server(use_ssl=False, server_port=http_port)
else:
# Normaler Start mit einem Protokoll
app_logger.info(f"Server wird auf Port {port} mit {'HTTPS' if ssl_context else 'HTTP'} gestartet...")
start_server(use_ssl=bool(ssl_context))
# Content Security Policy anpassen # Content Security Policy anpassen
@app.after_request @app.after_request
def add_security_headers(response): def add_security_headers(response):

View File

@ -36,7 +36,7 @@ SESSION_LIFETIME = timedelta(days=7)
SSL_ENABLED = True SSL_ENABLED = True
SSL_CERT_PATH = "instance/ssl/myp.crt" SSL_CERT_PATH = "instance/ssl/myp.crt"
SSL_KEY_PATH = "instance/ssl/myp.key" SSL_KEY_PATH = "instance/ssl/myp.key"
SSL_HOSTNAME = "localhost" SSL_HOSTNAME = "raspberrypi"
# Scheduler-Konfiguration # Scheduler-Konfiguration
SCHEDULER_INTERVAL = 60 # Sekunden SCHEDULER_INTERVAL = 60 # Sekunden

Binary file not shown.

View File

@ -23,8 +23,9 @@ services:
- FLASK_ENV=production - FLASK_ENV=production
- SSL_ENABLED=true - SSL_ENABLED=true
- SSL_HOSTNAME=raspberrypi - SSL_HOSTNAME=raspberrypi
command: python -m app.app --dual-protocol
healthcheck: healthcheck:
test: ["CMD", "curl", "-k", "https://localhost:443/health"] test: ["CMD", "curl", "-k", "https://localhost:443/health || curl http://localhost:80/health"]
interval: 30s interval: 30s
timeout: 10s timeout: 10s
retries: 3 retries: 3