From faf5f50ceaab3fe2587c76be2cf3bb58725cb513 Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Thu, 28 Nov 2024 23:39:18 +0800 Subject: [PATCH] is_enabled --- api/app_factory.py | 9 +++++++-- api/extensions/ext_compress.py | 19 ++++++++++--------- api/extensions/ext_mail.py | 2 ++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/api/app_factory.py b/api/app_factory.py index 42c0c4a2786e3d..32b1e6c725b92b 100644 --- a/api/app_factory.py +++ b/api/app_factory.py @@ -86,9 +86,14 @@ def initialize_extensions(app: DifyApp): ext_commands, ] for ext in extensions: + short_name = ext.__name__.split(".")[1] + is_enabled = ext.is_enabled() if hasattr(ext, "is_enabled") else True + if not is_enabled: + if dify_config.DEBUG: + logging.info(f"Skipped loading {short_name}") + start_time = time.perf_counter() ext.init_app(app) end_time = time.perf_counter() - ext_short_name = ext.__name__.split(".")[1] if dify_config.DEBUG: - logging.info(f"Loaded {ext_short_name} ({round((end_time - start_time) * 1000, 2)} ms)") + logging.info(f"Loaded {short_name} ({round((end_time - start_time) * 1000, 2)} ms)") diff --git a/api/extensions/ext_compress.py b/api/extensions/ext_compress.py index c4cad7b9f63b84..c4384e6b6ba753 100644 --- a/api/extensions/ext_compress.py +++ b/api/extensions/ext_compress.py @@ -1,16 +1,17 @@ from configs import dify_config from dify_app import DifyApp +def is_enabled() -> bool: + return dify_config.API_COMPRESSION_ENABLED def init_app(app: DifyApp): - if dify_config.API_COMPRESSION_ENABLED: - from flask_compress import Compress + from flask_compress import Compress - app.config["COMPRESS_MIMETYPES"] = [ - "application/json", - "image/svg+xml", - "text/html", - ] + app.config["COMPRESS_MIMETYPES"] = [ + "application/json", + "image/svg+xml", + "text/html", + ] - compress = Compress() - compress.init_app(app) + compress = Compress() + compress.init_app(app) diff --git a/api/extensions/ext_mail.py b/api/extensions/ext_mail.py index e984b7d03bc1e2..a7dbfe8158c451 100644 --- a/api/extensions/ext_mail.py +++ b/api/extensions/ext_mail.py @@ -85,6 +85,8 @@ def send(self, to: str, subject: str, html: str, from_: Optional[str] = None): } ) +def is_enabled() -> bool: + return dify_config.MAIL_TYPE is not None def init_app(app: DifyApp): mail.init_app(app)