From 552f319b9ddecd85cbe0d55d39a7a4527547874b Mon Sep 17 00:00:00 2001 From: Bowen Liang Date: Tue, 5 Mar 2024 14:45:22 +0800 Subject: [PATCH] feat: support HTTP response compression in api server (#2680) --- api/app.py | 2 ++ api/config.py | 2 ++ api/extensions/ext_compress.py | 10 ++++++++++ api/requirements.txt | 1 + 4 files changed, 15 insertions(+) create mode 100644 api/extensions/ext_compress.py diff --git a/api/app.py b/api/app.py index ac8bf27df1..aea28ac93a 100644 --- a/api/app.py +++ b/api/app.py @@ -26,6 +26,7 @@ from config import CloudEditionConfig, Config from extensions import ( ext_celery, ext_code_based_extension, + ext_compress, ext_database, ext_hosting_provider, ext_login, @@ -96,6 +97,7 @@ def create_app(test_config=None) -> Flask: def initialize_extensions(app): # Since the application instance is now created, pass it to each Flask # extension instance to bind it to the Flask application instance (app) + ext_compress.init_app(app) ext_code_based_extension.init() ext_database.init_app(app) ext_migrate.init(app, db) diff --git a/api/config.py b/api/config.py index faf4185c9a..7c46426b47 100644 --- a/api/config.py +++ b/api/config.py @@ -293,6 +293,8 @@ class Config: self.BATCH_UPLOAD_LIMIT = get_env('BATCH_UPLOAD_LIMIT') + self.API_COMPRESSION_ENABLED = get_bool_env('API_COMPRESSION_ENABLED') + class CloudEditionConfig(Config): diff --git a/api/extensions/ext_compress.py b/api/extensions/ext_compress.py new file mode 100644 index 0000000000..caa61675fb --- /dev/null +++ b/api/extensions/ext_compress.py @@ -0,0 +1,10 @@ +from flask import Flask + + +def init_app(app: Flask): + if app.config.get('API_COMPRESSION_ENABLED', False): + from flask_compress import Compress + + compress = Compress() + compress.init_app(app) + diff --git a/api/requirements.txt b/api/requirements.txt index 1c3e89e780..e44c8c9410 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -3,6 +3,7 @@ beautifulsoup4==4.12.2 flask~=3.0.1 Flask-SQLAlchemy~=3.0.5 SQLAlchemy~=1.4.28 +Flask-Compress~=1.14 flask-login~=0.6.3 flask-migrate~=4.0.5 flask-restful~=0.3.10