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(): 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) } app.config["DEBUG"] = os.environ.get("APP_DEBUG", "false").lower() in ("true", "1", "yes") 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