Files
gg 24b492c5c1 feat(runserver): Selbst-supervidierenden Dev-Server, -D-Parameter funktional
- Supervisor-Modus im Dev-Betrieb: Der Server läuft als Subprozess und
  wird nach einem Abbruch neu gestartet. Der Werkzeug-Reloader beendet
  sich sonst komplett, wenn der App-Prozess beim Start crasht — als
  Hauptprozess des Containers würde damit der gesamte Dev-Container
  stoppen. Der Supervisor importiert die App nicht und überlebt so
  jeden Anwendungsfehler.
- -D-Parameter ist jetzt funktional: Dev-Start direkt von der
  Befehlszeile ohne Devcontainer/Env-Variable. Debug-Entscheidung:
  -D ODER APP_DEBUG.
- approot.py: init_app(debug) reicht die Debug-Entscheidung durch,
  damit Server-Typ und JS-Bundle-Auswahl (.js/.min.js) konsistent
  bleiben. wsgi.py (init_app() ohne Argument) unverändert.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-09-22 19:12:27 +02:00

71 lines
2.7 KiB
Python

from flask import Flask, jsonify
from flask_login import LoginManager
from settings import Configuration
from db import dbauth
from db import dbapp
from auth.model.checkdb import checkupdates as checkupdatesauth
import os
login_manager = LoginManager()
def init_app(debug: bool | None = None):
app = Flask(__name__)
config: Configuration = Configuration("/usr/opt/pythonapp/App_Server/root/config.ini")
_projektname = config.app_projectname
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////usr/var/pythonappdb/{}.db".format(_projektname)
app.config["SQLALCHEMY_BINDS"] = {
"auth": "sqlite:////usr/var/pythonappdb/auth.db",
"appdb": "sqlite:////usr/var/pythonappdb/{}.db".format(_projektname)
}
# Dev-Modus: CLI-Parameter -D (via runserver) oder APP_DEBUG-Env.
# debug=None = wie bisher allein über die Env-Variable entscheiden
# (unverändertes Verhalten für wsgi.py).
if debug is None:
app.config["DEBUG"] = os.environ.get("APP_DEBUG", "false").lower() in ("true", "1", "yes")
else:
app.config["DEBUG"] = debug
app.config["PROJEKTNAME"] = config.app_projectname
app.config["APPID"] = config.app_appid
app.config["APPVERSION"] = config.clientapp_version
app.config["SECRET_KEY"] = config.flask_secret
# Initialize Plugins
# dbapp ist dasselbe Objekt wie dbauth (siehe db.py); beide teilen sich
# die SQLAlchemy-Registry. Daher genügt ein einziger init_app-Aufruf, der
# sowohl den auth- als auch den appdb-Bind initialisiert.
login_manager.init_app(app)
dbauth.init_app(app)
# Globale Fehlerbehandlung für ApplicationExceptionen. Die Route-internen
# try/except-Blöcke (auth) fangen weiterhin lokal ab; für alle nicht
# gefangenen Fälle (insbesondere crm) greift dieser Handler.
from auth.model.errors import ApplicationError, AutorisationError
@app.errorhandler(ApplicationError)
def handle_application_error(ex):
return jsonify(ex.error), ex.htmlstatus
@app.errorhandler(AutorisationError)
def handle_autorisation_error(ex):
return jsonify(ex.error), ex.htmlstatus
with app.app_context():
# Include our Routes
print("before auth.routes", flush=True)
from auth import routes
from routes import general
from crm import routes as crm_routes
from crm.model import checkdb as crm_checkdb
checkupdatesauth()
crm_checkdb.checkupdates()
# Flask CLI-Befehle (init-db, reset-db) registrieren
from cli import register_cli_commands
register_cli_commands(app)
return app