From 1f98a4fff382e0356dfdd135fb7aeaf8c3c7fc2e Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Tue, 26 Mar 2024 10:11:43 +0800 Subject: [PATCH] improve: cache tool icons by setting max-age HTTP header and enable gzip compression SVG icons from backend (#2971) --- api/config.py | 4 +++- api/controllers/console/workspace/tool_providers.py | 5 +++-- api/extensions/ext_compress.py | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/api/config.py b/api/config.py index 61ccdfee7b..d602db5c2c 100644 --- a/api/config.py +++ b/api/config.py @@ -59,7 +59,8 @@ DEFAULTS = { 'CAN_REPLACE_LOGO': 'False', 'ETL_TYPE': 'dify', 'KEYWORD_STORE': 'jieba', - 'BATCH_UPLOAD_LIMIT': 20 + 'BATCH_UPLOAD_LIMIT': 20, + 'TOOL_ICON_CACHE_MAX_AGE': 3600, } @@ -298,6 +299,7 @@ class Config: self.BATCH_UPLOAD_LIMIT = get_env('BATCH_UPLOAD_LIMIT') self.API_COMPRESSION_ENABLED = get_bool_env('API_COMPRESSION_ENABLED') + self.TOOL_ICON_CACHE_MAX_AGE = get_env('TOOL_ICON_CACHE_MAX_AGE') class CloudEditionConfig(Config): diff --git a/api/controllers/console/workspace/tool_providers.py b/api/controllers/console/workspace/tool_providers.py index 931979c7f3..f9c2bc8d1c 100644 --- a/api/controllers/console/workspace/tool_providers.py +++ b/api/controllers/console/workspace/tool_providers.py @@ -1,6 +1,6 @@ import io -from flask import send_file +from flask import current_app, send_file from flask_login import current_user from flask_restful import Resource, reqparse from werkzeug.exceptions import Forbidden @@ -80,7 +80,8 @@ class ToolBuiltinProviderIconApi(Resource): @setup_required def get(self, provider): icon_bytes, minetype = ToolManageService.get_builtin_tool_provider_icon(provider) - return send_file(io.BytesIO(icon_bytes), mimetype=minetype) + icon_cache_max_age = int(current_app.config.get('TOOL_ICON_CACHE_MAX_AGE')) + return send_file(io.BytesIO(icon_bytes), mimetype=minetype, max_age=icon_cache_max_age) class ToolModelProviderIconApi(Resource): @setup_required diff --git a/api/extensions/ext_compress.py b/api/extensions/ext_compress.py index caa61675fb..4a349d37b4 100644 --- a/api/extensions/ext_compress.py +++ b/api/extensions/ext_compress.py @@ -5,6 +5,12 @@ def init_app(app: Flask): if app.config.get('API_COMPRESSION_ENABLED', False): from flask_compress import Compress + app.config['COMPRESS_MIMETYPES'] = [ + 'application/json', + 'image/svg+xml', + 'text/html', + ] + compress = Compress() compress.init_app(app)