def create_app(settings_override: Optional[dict] = None) -> Flask: if settings_override is None: settings_override = {} app = Flask(__name__) app.json_encoder = JSONEncoder app.register_blueprint(auctions_blueprint, url_prefix="/auctions") app.register_blueprint(shipping_blueprint, url_prefix="/shipping") # TODO: move this config app.config["SECRET_KEY"] = "super-secret" app.config["DEBUG"] = True app.config["SECURITY_SEND_REGISTER_EMAIL"] = False app.config["SECURITY_REGISTERABLE"] = True app.config[ "SECURITY_PASSWORD_SALT"] = "99f885320c0f867cde17876a7849904c41a2b8120a9a9e76d1789e458e543af9" app.config["WTF_CSRF_ENABLED"] = False for key, value in settings_override.items(): app.config[key] = value app_context = bootstrap_app() FlaskInjector(app, modules=[AuctionsWeb()], injector=app_context.injector) app.injector = app_context.injector @app.before_request def transaction_start() -> None: app_context.injector.get(RequestScope).enter() request.connection = app_context.injector.get( Connection) # type: ignore request.tx = request.connection.begin() # type: ignore request.session = app_context.injector.get(Session) # type: ignore @app.after_request def transaction_commit(response: Response) -> Response: scope = app_context.injector.get(RequestScope) try: if hasattr(request, "tx") and response.status_code < 400: request.tx.commit() # type: ignore finally: scope.exit() return response @app.after_request def add_cors_headers(response: Response) -> Response: response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Headers"] = "*" return response # has to be done after DB-hooks, because it relies on DB security_setup(app) return app
def create_app() -> Flask: app = Flask(__name__) app.json_encoder = JSONEncoder app.register_blueprint(auctions_blueprint, url_prefix="/auctions") app.register_blueprint(shipping_blueprint, url_prefix="/shipping") # TODO: move this config app.config["SECRET_KEY"] = "super-secret" app.config["DEBUG"] = True app.config["SECURITY_SEND_REGISTER_EMAIL"] = False app.config["SECURITY_REGISTERABLE"] = True app.config[ "SECURITY_PASSWORD_SALT"] = "99f885320c0f867cde17876a7849904c41a2b8120a9a9e76d1789e458e543af9" app.config["WTF_CSRF_ENABLED"] = False app_context = bootstrap_app() FlaskInjector(app, modules=[AuctionsWeb()], injector=app_context.injector) @app.before_request def transaction_start() -> None: request.tx = app_context.connection_provider.open().begin() request.session = app_context.connection_provider.provide_session() @app.after_request def transaction_commit(response: Response) -> Response: try: if hasattr(request, "tx") and response.status_code < 400: request.tx.commit() finally: app_context.connection_provider.close_if_present() return response @app.after_request def add_cors_headers(response: Response) -> Response: response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Headers"] = "*" return response # has to be after DB-hooks, because it relies on DB security_setup(app) return app
def async_handler_generic_task(cls, *args, **kwargs): # type: ignore """ This function is meant to be used for running asynchronous event handlers. It bootstraps application context and wraps task with DB transaction. """ from main import bootstrap_app from main.modules import RequestScope app = bootstrap_app() scope = app.injector.get(RequestScope) scope.enter() connection = app.injector.get(Connection) try: with connection.begin(): instance = app.injector.create_object(cls) instance(*args, **kwargs) finally: scope.exit()
from flask_script import Manager, Server, Shell from main import bootstrap_app from broker_rabbit.manager import broker_rabbit_manager app = bootstrap_app() manager = Manager(app) manager.add_command("runserver", Server()) manager.add_command("shell", Shell()) manager.add_command("broker", broker_rabbit_manager) if __name__ == "__main__": manager.run()