Skip to content

Commit

Permalink
add cachecontrol_exclude_paths in ApiSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Aug 22, 2024
1 parent 3582099 commit d06393a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## Unreleased

* add `cachecontrol_exclude_paths` attribute in `ApiSettings` to let users decide if some path should not have cache-control headers (defaults is to exclude `/list`)

## 1.3.1 (2024-08-01)

* update models to avoid pydantic deprecation
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def app(database_url, monkeypatch):
from titiler.pgstac.main import app

# Remove middlewares https://github.com/encode/starlette/issues/472
app.user_middleware = []
app.middleware_stack = app.build_middleware_stack()
# app.user_middleware = []
# app.middleware_stack = app.build_middleware_stack()

with TestClient(app) as app:
yield app
13 changes: 13 additions & 0 deletions tests/test_searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,16 @@ def test_query_point_searches(app, search_no_bbox, search_bbox):
)

assert response.status_code == 204 # (no content)


def test_cache_middleware_settings(app, search_no_bbox):
"""Make sure some endpoints do not have cache-control headers."""
response = app.get("/searches/list")
assert response.status_code == 200
assert not response.headers.get("Cache-Control")

response = app.get(
f"/searches/{search_no_bbox}/point/-85.5,36.1624", params={"assets": "cog"}
)
assert response.status_code == 200
assert response.headers.get("Cache-Control")
6 changes: 5 additions & 1 deletion titiler/pgstac/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ async def lifespan(app: FastAPI):
allow_headers=["*"],
)

app.add_middleware(CacheControlMiddleware, cachecontrol=settings.cachecontrol)
app.add_middleware(
CacheControlMiddleware,
cachecontrol=settings.cachecontrol,
exclude_path=settings.cachecontrol_exclude_paths,
)

optional_headers = []
if settings.debug:
Expand Down
7 changes: 6 additions & 1 deletion titiler/pgstac/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""API settings."""

from functools import lru_cache
from typing import Any, Optional
from typing import Any, Optional, Set

from pydantic import (
Field,
Expand All @@ -20,6 +20,11 @@ class ApiSettings(BaseSettings):
name: str = "titiler-pgstac"
cors_origins: str = "*"
cachecontrol: str = "public, max-age=3600"
cachecontrol_exclude_paths: Set[str] = Field(
default={
".+/list",
}
)
root_path: str = ""
debug: bool = False

Expand Down

0 comments on commit d06393a

Please sign in to comment.