def create_app() -> flask.Flask: """Create a broker flask app. """ # pylint: disable=redefined-outer-name, import-outside-toplevel from . import api, exceptions, models, tasks, admin_panel import cg_timers app = BrokerFlask(__name__) cg_logger.init_app(app, set_user=False) models.init_app(app) api.init_app(app) tasks.init_app(app) exceptions.init_app(app) admin_panel.init_app(app) cg_timers.init_app(app) if app.debug: tasks.add_1.delay(1, 2) return app
def create_app( # pylint: disable=too-many-statements config: t.Mapping = None, skip_celery: bool = False, skip_perm_check: bool = True, skip_secret_key_check: bool = False, *, skip_all: bool = False, ) -> 'PsefFlask': """Create a new psef app. :param config: The config mapping that can be used to override config. :param skip_celery: Set to true to disable sanity checks for celery. :returns: A new psef app object. """ # pylint: disable=import-outside-toplevel import config as global_config if skip_all: # pragma: no cover skip_celery = skip_perm_check = skip_secret_key_check = True resulting_app = PsefFlask( __name__, { **global_config.CONFIG, **(config or {}), }, ) resulting_app.config['SQLALCHEMY_TRACK_MODIFICATIONS' # type: ignore ] = False resulting_app.config['SESSION_COOKIE_SECURE'] = True resulting_app.config['SESSION_COOKIE_SAMESITE'] = 'None' # We don't cache responses from the API so it makes no sense to sort the # keys. resulting_app.config['JSON_SORT_KEYS'] = False if not resulting_app.debug: assert not resulting_app.config['AUTO_TEST_DISABLE_ORIGIN_CHECK'] if ( not skip_secret_key_check and not ( resulting_app.config['SECRET_KEY'] and resulting_app.config['LTI_SECRET_KEY'] ) ): # pragma: no cover raise ValueError('The option to generate keys has been removed') from . import limiter limiter.init_app(resulting_app) cg_logger.init_app(resulting_app) from . import permissions permissions.init_app(resulting_app, skip_perm_check) from . import auth auth.init_app(resulting_app) from . import tasks tasks.init_app(resulting_app) from . import models models.init_app(resulting_app) from . import mail mail.init_app(resulting_app) from . import errors errors.init_app(resulting_app) from . import files files.init_app(resulting_app) from . import lti lti.init_app(resulting_app) from . import auto_test auto_test.init_app(resulting_app) from . import helpers helpers.init_app(resulting_app) from . import linters linters.init_app(resulting_app) from . import plagiarism plagiarism.init_app(resulting_app) # Register blueprint(s) from . import v1 as api_v1 api_v1.init_app(resulting_app) from . import v_internal as api_v_internal api_v_internal.init_app(resulting_app) from . import signals signals.init_app(resulting_app) from . import saml2 saml2.init_app(resulting_app) from . import site_settings site_settings.init_app(resulting_app) # Make sure celery is working if not skip_celery: # pragma: no cover try: tasks.add(2, 3) except Exception: # pragma: no cover logger.error('Celery is not responding! Please check your config') raise import cg_timers cg_timers.init_app(resulting_app) cg_cache.init_app(resulting_app) return resulting_app
def create_app( # pylint: disable=too-many-statements config: t.Mapping = None, skip_celery: bool = False, skip_perm_check: bool = True, skip_secret_key_check: bool = False, *, skip_all: bool = False, ) -> 'PsefFlask': """Create a new psef app. :param config: The config mapping that can be used to override config. :param skip_celery: Set to true to disable sanity checks for celery. :returns: A new psef app object. """ # pylint: disable=import-outside-toplevel import config as global_config if skip_all: # pragma: no cover skip_celery = skip_perm_check = skip_secret_key_check = True resulting_app = PsefFlask( __name__, { **global_config.CONFIG, **(config or {}), }, ) resulting_app.config['SQLALCHEMY_TRACK_MODIFICATIONS' # type: ignore ] = False resulting_app.config['SESSION_COOKIE_SECURE'] = True resulting_app.config['SESSION_COOKIE_SAMESITE'] = 'None' if not resulting_app.debug: assert not resulting_app.config['AUTO_TEST_DISABLE_ORIGIN_CHECK'] if (not skip_secret_key_check and not (resulting_app.config['SECRET_KEY'] and resulting_app.config['LTI_SECRET_KEY'])): # pragma: no cover raise ValueError('The option to generate keys has been removed') @resulting_app.errorhandler(RateLimitExceeded) def __handle_error(_: RateLimitExceeded) -> Response: # pylint: disable=unused-variable res = t.cast( Response, jsonify( errors.APIException( 'Rate limit exceeded, slow down!', 'Rate limit is exceeded', errors.APICodes.RATE_LIMIT_EXCEEDED, 429, ))) res.status_code = 429 return res limiter.init_app(resulting_app) cg_logger.init_app(resulting_app) from . import permissions permissions.init_app(resulting_app, skip_perm_check) from . import features features.init_app(resulting_app) from . import auth auth.init_app(resulting_app) from . import parsers parsers.init_app(resulting_app) from . import tasks tasks.init_app(resulting_app) from . import models models.init_app(resulting_app) from . import mail mail.init_app(resulting_app) from . import errors errors.init_app(resulting_app) from . import files files.init_app(resulting_app) from . import lti lti.init_app(resulting_app) from . import auto_test auto_test.init_app(resulting_app) from . import helpers helpers.init_app(resulting_app) from . import linters linters.init_app(resulting_app) from . import plagiarism plagiarism.init_app(resulting_app) # Register blueprint(s) from . import v1 as api_v1 api_v1.init_app(resulting_app) from . import v_internal as api_v_internal api_v_internal.init_app(resulting_app) from . import signals signals.init_app(resulting_app) # Make sure celery is working if not skip_celery: # pragma: no cover try: tasks.add(2, 3) except Exception: # pragma: no cover logger.error('Celery is not responding! Please check your config') raise import cg_timers cg_timers.init_app(resulting_app) cg_cache.init_app(resulting_app) return resulting_app