Skip to content

Commit

Permalink
use starlette_cramjam compression middleware (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Sep 20, 2021
1 parent 7cf3b61 commit 381685e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
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

0 comments on commit 381685e

Please sign in to comment.