From d94f5428a8dc71bb482e16c1a79f876d45462322 Mon Sep 17 00:00:00 2001 From: Michael Manganiello Date: Sat, 13 Jul 2024 16:10:46 -0300 Subject: [PATCH] misc: Enable GZip compression for backend responses With the introduction of non-paginated responses for ROMs, JSON responses for big collection could easily be at >1MiB. This change adds a FastAPI-provided middleware to enable GZip compression for responses greater than 1KiB. --- backend/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/main.py b/backend/main.py index d8281b0f7..7d1c639a8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -28,6 +28,7 @@ from endpoints import ( ) from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware +from fastapi.middleware.gzip import GZipMiddleware from handler.auth.base_handler import ALGORITHM from handler.auth.hybrid_auth import HybridAuthBackend from handler.auth.middleware import CustomCSRFMiddleware, SessionMiddleware @@ -61,6 +62,9 @@ if "pytest" not in sys.modules and not DISABLE_CSRF_PROTECTION: exempt_urls=[re.compile(r"^/token.*"), re.compile(r"^/ws")], ) +# Enable GZip compression for responses +app.add_middleware(GZipMiddleware, minimum_size=1024) + # Handles both basic and oauth authentication app.add_middleware( AuthenticationMiddleware,