Skip to content

Commit 4d483fd

Browse files
Bud-Macaulaypre-commit-ci[bot]ml-evs
authored
Add GZipMiddleware to compress JSON responses on-the-fly (#2308)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Matthew Evans <git@ml-evs.science>
1 parent 5ed8a42 commit 4d483fd

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

optimade/server/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import yaml
99
from pydantic import (
1010
AnyHttpUrl,
11+
BaseModel,
1112
Field,
1213
NonNegativeFloat,
1314
field_validator,
@@ -145,6 +146,25 @@ def __call__(self) -> dict[str, Any]:
145146
return self.parse_config_file()
146147

147148

149+
class GZipConfig(BaseModel):
150+
enabled: Annotated[
151+
bool,
152+
Field(description="Enable GZip compression for API responses."),
153+
] = False
154+
155+
minimum_size: Annotated[
156+
int,
157+
Field(
158+
description="Minimum response size (in bytes) before compression is applied."
159+
),
160+
] = 5000
161+
162+
compresslevel: Annotated[
163+
int,
164+
Field(description="Compression level (1=fastest, 9=best compression)."),
165+
] = 3
166+
167+
148168
class ServerConfig(BaseSettings):
149169
"""This class stores server config parameters in a way that
150170
can be easily extended for new config file types.
@@ -199,6 +219,11 @@ class ServerConfig(BaseSettings):
199219
),
200220
] = False
201221

222+
gzip: Annotated[
223+
GZipConfig,
224+
Field(description="Configuration options for GZip compression."),
225+
] = GZipConfig()
226+
202227
use_real_mongo: Annotated[
203228
bool | None,
204229
Field(description="DEPRECATED: force usage of MongoDB over any other backend."),

optimade/server/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from fastapi import FastAPI
1515
from fastapi.middleware.cors import CORSMiddleware
16+
from fastapi.middleware.gzip import GZipMiddleware
1617

1718
with warnings.catch_warnings(record=True) as w:
1819
from optimade.server.config import CONFIG, DEFAULT_CONFIG_FILE_PATH
@@ -144,6 +145,15 @@ def load_entries(endpoint_name: str, endpoint_collection: EntryCollection):
144145
for middleware in OPTIMADE_MIDDLEWARE:
145146
app.add_middleware(middleware)
146147

148+
# Enable GZIP after other middleware.
149+
if CONFIG.gzip.enabled:
150+
app.add_middleware(
151+
GZipMiddleware,
152+
minimum_size=CONFIG.gzip.minimum_size,
153+
compresslevel=CONFIG.gzip.compresslevel,
154+
)
155+
156+
147157
# Add exception handlers
148158
for exception, handler in OPTIMADE_EXCEPTIONS:
149159
app.add_exception_handler(exception, handler)

tests/test_config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"debug": false,
33
"default_db": "test_server",
44
"base_url": "http://example.org",
5+
"gzip": {"enabled": true},
56
"implementation": {
67
"name": "Example implementation",
78
"source_url": "https://github.com/Materials-Consortia/optimade-python-tools",

0 commit comments

Comments
 (0)