import logging from logging.handlers import RotatingFileHandler import os from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_wtf.csrf import CSRFProtect from config import Config db = SQLAlchemy() migrate = Migrate() csrf = CSRFProtect() def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) # Initialisierung der Erweiterungen db.init_app(app) migrate.init_app(app, db) csrf.init_app(app) # Logging-Konfiguration if not app.debug and not app.testing: if not os.path.exists('logs'): os.mkdir('logs') file_handler = RotatingFileHandler('logs/app.log', maxBytes=10240, backupCount=10) file_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]' )) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Anwendung startet') # Blueprints registrieren from app.routes.main import bp as main_bp app.register_blueprint(main_bp) # Fehlerbehandlung @app.errorhandler(404) def not_found_error(error): return render_template('errors/404.html'), 404 @app.errorhandler(500) def internal_error(error): db.session.rollback() return render_template('errors/500.html'), 500 @app.context_processor def utility_processor(): return dict(enumerate=enumerate) return app