mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
chore: proper current_app.config proxy usage (#34345)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
committed by
GitHub
parent
6c9cda758a
commit
cb27d5fe8d
@@ -23,6 +23,7 @@ import sys
|
||||
from typing import Any, Callable, TYPE_CHECKING
|
||||
|
||||
import wtforms_json
|
||||
from colorama import Fore, Style
|
||||
from deprecation import deprecated
|
||||
from flask import abort, Flask, redirect, request, session, url_for
|
||||
from flask_appbuilder import expose, IndexView
|
||||
@@ -201,6 +202,12 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
from superset.views.users_list import UsersListView
|
||||
|
||||
set_app_error_handlers(self.superset_app)
|
||||
self.register_request_handlers()
|
||||
|
||||
# Register health blueprint
|
||||
from superset.views.health import health_blueprint
|
||||
|
||||
self.superset_app.register_blueprint(health_blueprint)
|
||||
|
||||
#
|
||||
# Setup API views
|
||||
@@ -552,6 +559,54 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
if self.config["SESSION_SERVER_SIDE"]:
|
||||
Session(self.superset_app)
|
||||
|
||||
def register_request_handlers(self) -> None:
|
||||
"""Register app-level request handlers"""
|
||||
from flask import Response
|
||||
|
||||
@self.superset_app.after_request
|
||||
def apply_http_headers(response: Response) -> Response:
|
||||
"""Applies the configuration's http headers to all responses"""
|
||||
# HTTP_HEADERS is deprecated, this provides backwards compatibility
|
||||
response.headers.extend(
|
||||
{
|
||||
**self.superset_app.config["OVERRIDE_HTTP_HEADERS"],
|
||||
**self.superset_app.config["HTTP_HEADERS"],
|
||||
}
|
||||
)
|
||||
|
||||
for k, v in self.superset_app.config["DEFAULT_HTTP_HEADERS"].items():
|
||||
if k not in response.headers:
|
||||
response.headers[k] = v
|
||||
return response
|
||||
|
||||
@self.superset_app.context_processor
|
||||
def get_common_bootstrap_data() -> dict[str, Any]:
|
||||
# Import here to avoid circular imports
|
||||
from superset.utils import json
|
||||
from superset.views.base import common_bootstrap_payload
|
||||
|
||||
def serialize_bootstrap_data() -> str:
|
||||
return json.dumps(
|
||||
{"common": common_bootstrap_payload()},
|
||||
default=json.pessimistic_json_iso_dttm_ser,
|
||||
)
|
||||
|
||||
return {"bootstrap_data": serialize_bootstrap_data}
|
||||
|
||||
def check_and_warn_database_connection(self) -> None:
|
||||
"""Check database connection and warn if unavailable"""
|
||||
try:
|
||||
with self.superset_app.app_context():
|
||||
# Simple connection test
|
||||
db.engine.execute("SELECT 1")
|
||||
except Exception:
|
||||
db_uri = self.config.get("SQLALCHEMY_DATABASE_URI", "")
|
||||
safe_uri = make_url_safe(db_uri) if db_uri else "Not configured"
|
||||
print(
|
||||
f"{Fore.RED}ERROR: Cannot connect to database {safe_uri}\n"
|
||||
f"NOTE: Most CLI commands require a database{Style.RESET_ALL}"
|
||||
)
|
||||
|
||||
def init_app(self) -> None:
|
||||
"""
|
||||
Main entry point which will delegate to other methods in
|
||||
@@ -567,6 +622,10 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
self.configure_feature_flags()
|
||||
self.configure_db_encrypt()
|
||||
self.setup_db()
|
||||
|
||||
# Check database connection and warn if unavailable
|
||||
self.check_and_warn_database_connection()
|
||||
|
||||
self.configure_celery()
|
||||
self.enable_profiling()
|
||||
self.setup_event_logger()
|
||||
@@ -599,7 +658,7 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
set_isolation_level_to = "READ COMMITTED"
|
||||
|
||||
if set_isolation_level_to:
|
||||
logger.info(
|
||||
logger.debug(
|
||||
"Setting database isolation level to %s",
|
||||
set_isolation_level_to,
|
||||
)
|
||||
@@ -777,6 +836,7 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
async_query_manager_factory.init_app(self.superset_app)
|
||||
|
||||
def register_blueprints(self) -> None:
|
||||
# Register custom blueprints from config
|
||||
for bp in self.config["BLUEPRINTS"]:
|
||||
try:
|
||||
logger.info("Registering blueprint: %s", bp.name)
|
||||
|
||||
Reference in New Issue
Block a user