Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use starlette_cramjam compression middleware #369

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes

## 0.3.10 (TBD)

### titiler.application

- switch to `starlette_cramjam` compression middleware (ref: https://github.com/developmentseed/titiler/issues/369)

## 0.3.9 (2021-09-07)

### titiler.core
Expand Down
4 changes: 2 additions & 2 deletions src/titiler/application/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"rio-cogeo>=2.2",
"titiler.core",
"titiler.mosaic",
"brotli-asgi>=1.0.0",
"starlette-cramjam>=0.1.0,<0.2",
"python-dotenv",
]
extra_reqs = {
"test": ["pytest", "pytest-cov", "pytest-asyncio", "requests"],
"test": ["pytest", "pytest-cov", "pytest-asyncio", "requests", "brotlipy"],
"server": ["uvicorn[standard]>=0.12.0,<0.14.0"],
}

Expand Down
9 changes: 9 additions & 0 deletions src/titiler/application/tests/routes/test_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ def test_tile(rio, app):
assert response.status_code == 200
assert response.headers["content-encoding"] == "br"

# Exclude png from compression middleware
headers = {"Accept-Encoding": "br, gzip"}
response = app.get(
"/cog/tiles/8/87/48.png?url=https://myurl.com/cog.tif&nodata=0&return_mask=false",
headers=headers,
)
assert response.status_code == 200
assert "content-encoding" not in response.headers

# Test gzip fallback
headers = {"Accept-Encoding": "gzip"}
response = app.get(
Expand Down
16 changes: 13 additions & 3 deletions src/titiler/application/titiler/application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import logging

from brotli_asgi import BrotliMiddleware

from titiler.application.custom import templates
from titiler.application.routers import cog, mosaic, stac, tms
from titiler.application.settings import ApiSettings
Expand All @@ -22,6 +20,7 @@
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette_cramjam.middleware import CompressionMiddleware

logging.getLogger("botocore.credentials").disabled = True
logging.getLogger("botocore.utils").disabled = True
Expand Down Expand Up @@ -62,7 +61,18 @@
allow_headers=["*"],
)

app.add_middleware(BrotliMiddleware, minimum_size=0, gzip_fallback=True)
app.add_middleware(
CompressionMiddleware,
minimum_size=0,
exclude_mediatype={
"image/jpeg",
"image/jpg",
"image/png",
"image/jp2",
"image/webp",
},
)

app.add_middleware(
CacheControlMiddleware,
cachecontrol=api_settings.cachecontrol,
Expand Down