john-bodley commented on a change in pull request #8418: Flask App factory PR #1 URL: https://github.com/apache/incubator-superset/pull/8418#discussion_r337127725
########## File path: superset/app.py ########## @@ -0,0 +1,251 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import logging +import os + +from flask_appbuilder import IndexView, expose +from flask_compress import Compress +from flask import Flask, redirect +from superset.extensions import ( + _event_logger, + APP_DIR, + appbuilder, + cache_manager, + db, + feature_flag_manager, + manifest_processor, + migrate, + results_backend_manager, + talisman +) +from flask_wtf import CSRFProtect + +from superset.connectors.connector_registry import ConnectorRegistry +from superset.security import SupersetSecurityManager +from superset.utils.core import pessimistic_connection_handling +from superset.utils.log import get_event_logger_from_cfg_value, DBEventLogger +import wtforms_json + + +logger = logging.getLogger(__name__) + + +def create_app(): + + + try: + # Allow user to override our config completely + config_module = os.environ.get("SUPERSET_CONFIG", "superset.config") + app.config.from_object(config_module) + + app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app) + app_initializer.init_app() + + return app + + # Make sure that bootstrap errors ALWAYS get logged + except Exception as ex: + logger.exception("Failed to create app") + raise ex + + +class SupersetIndexView(IndexView): + @expose("/") + def index(self): + return redirect("/superset/welcome") + + +class SupersetAppInitializer: + def __init__(self, app: Flask) -> None: + super().__init__() + + self.flask_app = app + self.config = app.config + self.manifest = {} + + def pre_init(self) -> None: + """ + Called after all other init tasks are complete + """ + wtforms_json.init() + + if not os.path.exists(self.config["DATA_DIR"]): + os.makedirs(self.config["DATA_DIR"]) + + def post_init(self) -> None: + """ + Called before any other init tasks + """ + pass + + def init_views(self) -> None: + # TODO - This should iterate over all views and register them with FAB... + from superset import views # noqa + + def init_app_in_ctx(self) -> None: + """ + Runs init logic in the context of the app + """ + self.configure_fab() + + # self.configure_data_sources() + + # Hook that provides administrators a handle on the Flask APP + # after initialization + flask_app_mutator = self.config.get("FLASK_APP_MUTATOR") + if flask_app_mutator: + flask_app_mutator(self.flask_app) + + self.init_views() + + def init_app(self) -> None: + """ + Main entry point which will delegate to other methods in + order to fully init the app + """ + self.pre_init() + + self.setup_db() + + self.setup_event_logger() + + self.setup_bundle_manifest() + + self.register_blueprints() + + self.configure_wtf() + + self.configure_logging() + + self.configure_middlewares() + + self.configure_cache() + + with self.flask_app.app_context(): + self.init_app_in_ctx() + + self.post_init() + + def setup_event_logger(self): + _event_logger["event_logger"] = get_event_logger_from_cfg_value( + self.flask_app.config.get("EVENT_LOGGER", DBEventLogger()) + ) + + def configure_data_sources(self): + # Registering sources + module_datasource_map = self.config.get("DEFAULT_MODULE_DS_MAP") + module_datasource_map.update(self.config.get("ADDITIONAL_MODULE_DS_MAP")) + ConnectorRegistry.register_sources(module_datasource_map) + + def configure_cache(self): + cache_manager.init_app(self.flask_app) + results_backend_manager.init_app(self.flask_app) + + def configure_feature_flags(self): + feature_flag_manager.init_app(self.flask_app) + + def configure_fab(self): + if self.config.get("SILENCE_FAB"): + logging.getLogger("flask_appbuilder").setLevel(logging.ERROR) + + custom_sm = self.config.get("CUSTOM_SECURITY_MANAGER") or SupersetSecurityManager + if not issubclass(custom_sm, SupersetSecurityManager): + raise Exception( + """Your CUSTOM_SECURITY_MANAGER must now extend SupersetSecurityManager, + not FAB's security manager. + See [4565] in UPDATING.md""" + ) + + appbuilder.indexview = SupersetIndexView + appbuilder.base_template = "superset/base.html" + appbuilder.security_manager_class = custom_sm + appbuilder.update_perms = False + appbuilder.init_app(self.flask_app, db.session) + + def configure_middlewares(self): + if self.config.get("ENABLE_CORS"): + from flask_cors import CORS + + CORS(self.flask_app, **self.config.get("CORS_OPTIONS")) + + if self.config.get("ENABLE_PROXY_FIX"): + from werkzeug.middleware.proxy_fix import ProxyFix + + self.flask_app.wsgi_app = ProxyFix(self.flask_app.wsgi_app, **self.config.get("PROXY_FIX_CONFIG")) + + if self.config.get("ENABLE_CHUNK_ENCODING"): + + class ChunkedEncodingFix(object): + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + # Setting wsgi.input_terminated tells werkzeug.wsgi to ignore + # content-length and read the stream till the end. + if environ.get("HTTP_TRANSFER_ENCODING", "").lower() == u"chunked": + environ["wsgi.input_terminated"] = True + return self.app(environ, start_response) + + self.flask_app.wsgi_app = ChunkedEncodingFix(self.flask_app.wsgi_app) + + if self.config.get("UPLOAD_FOLDER"): + try: + os.makedirs(self.config.get("UPLOAD_FOLDER")) + except OSError: + pass + + for middleware in self.config.get("ADDITIONAL_MIDDLEWARE"): + self.flask_app.wsgi_app = middleware(self.flask_app.wsgi_app) + + # Flask-Compress + if self.config.get("ENABLE_FLASK_COMPRESS"): + Compress(self.flask_app) + + if self.config["TALISMAN_ENABLED"]: + talisman.init_app(self.flask_app, **self.config["TALISMAN_CONFIG"]) + + def configure_logging(self): + self.flask_app.config.get("LOGGING_CONFIGURATOR").configure_logging( + self.config, + self.flask_app.debug) + + def setup_db(self): + db.init_app(self.flask_app) + + with self.flask_app.app_context(): + pessimistic_connection_handling(db.engine) + + migrate.init_app(self.flask_app, db=db, directory=APP_DIR + "/migrations") + + def configure_wtf(self): + if self.config.get("WTF_CSRF_ENABLED"): + csrf = CSRFProtect(self.flask_app) + csrf_exempt_list = self.config.get("WTF_CSRF_EXEMPT_LIST", []) + for ex in csrf_exempt_list: + csrf.exempt(ex) + + def register_blueprints(self): + for bp in self.config.get("BLUEPRINTS"): Review comment: `mypy` is going to barf at using `config.get(...)` as the return type is `Optional[...]`. In reference to my previous comment about the default config, `config[...]` should be suffice. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
