def METHOD_NAME(test_config=None):
    from flowauth.config import get_config
    from flowauth.models import db
    from flowauth.cli import (
        init_db_command,
        add_admin_command,
        make_flowauth_fernet_key,
        demo_data,
    )
    from .admin import blueprint as admin_blueprint
    from .users import blueprint as users_blueprint
    from .servers import blueprint as servers_blueprint
    from .token_management import blueprint as token_management_blueprint
    from .login import blueprint as login_blueprint
    from .user_settings import blueprint as user_settings_blueprint
    from .version import blueprint as version_blueprint
    from .roles import blueprint as roles_blueprint
    app = Flask(__name__)
    app.config.from_mapping(get_config())
    # Connect the logger
    app.before_first_request(connect_logger)
    if test_config is not None:
        # load the test config if passed in
        app.config.update(test_config)
    # Connect the db
    db.init_app(app)
    # Set up flask-login
    login_manager = LoginManager()
    login_manager.init_app(app)
    # Set up flask-principal for roles management
    principals = Principal()
    principals.init_app(app)
    # Set up csrf protection
    csrf = CSRFProtect()
    csrf.init_app(app)
    csrf.exempt(login_blueprint)
    csrf.exempt(version_blueprint)
    app.register_blueprint(login_blueprint)
    app.register_blueprint(admin_blueprint, url_prefix="/admin")
    app.register_blueprint(servers_blueprint, url_prefix="/admin")
    app.register_blueprint(users_blueprint, url_prefix="/admin")
    app.register_blueprint(token_management_blueprint, url_prefix="/tokens")
    app.register_blueprint(user_settings_blueprint, url_prefix="/user")
    app.register_blueprint(roles_blueprint, url_prefix="/roles")
    app.register_blueprint(version_blueprint)
    if app.config["DEMO_MODE"]:  # Create demo data
        from flowauth.models import make_demodata
        app.before_first_request(make_demodata)
    else:
        # Initialise the database
        from flowauth.models import init_db
        from flowauth.models import add_admin
        app.before_first_request(lock(partial(init_db, force=app.config["RESET_DB"])))
        # Create an admin user
        app.before_first_request(
            lock(
                partial(
                    add_admin,
                    username=app.config["ADMIN_USER"],
                    password=app.config["ADMIN_PASSWORD"],
                )
            )
        )
    app.before_first_request(
        app.config["DB_IS_SET_UP"].wait
    )  # Cause workers to wait for db to set up
    # Register for migrations
    migrate = Migrate(app, db)
    app.after_request(set_xsrf_cookie)
    app.errorhandler(CSRFError)(handle_csrf_error)
    app.errorhandler(InvalidUsage)(handle_invalid_usage)
    app.errorhandler(IntegrityError)(handle_unique_error)
    app.before_request(before_request)
    login_manager.user_loader(load_user)
    identity_loaded.connect_via(app)(on_identity_loaded)
    # Add flask <command> CLI commands
    app.cli.add_command(demo_data)
    app.cli.add_command(init_db_command)
    app.cli.add_command(add_admin_command)
    app.cli.add_command(make_flowauth_fernet_key)
    return app