Skip to content

Commit d353b9d

Browse files
Merge pull request #178 from stac-utils/feature/add-exclude-paths-cachecontrol
add cachecontrol_exclude_paths in ApiSettings
2 parents 3582099 + d06393a commit d353b9d

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## Unreleased
4+
5+
* 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`)
6+
37
## 1.3.1 (2024-08-01)
48

59
* update models to avoid pydantic deprecation

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def app(database_url, monkeypatch):
101101
from titiler.pgstac.main import app
102102

103103
# Remove middlewares https://github.com/encode/starlette/issues/472
104-
app.user_middleware = []
105-
app.middleware_stack = app.build_middleware_stack()
104+
# app.user_middleware = []
105+
# app.middleware_stack = app.build_middleware_stack()
106106

107107
with TestClient(app) as app:
108108
yield app

tests/test_searches.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,3 +1043,16 @@ def test_query_point_searches(app, search_no_bbox, search_bbox):
10431043
)
10441044

10451045
assert response.status_code == 204 # (no content)
1046+
1047+
1048+
def test_cache_middleware_settings(app, search_no_bbox):
1049+
"""Make sure some endpoints do not have cache-control headers."""
1050+
response = app.get("/searches/list")
1051+
assert response.status_code == 200
1052+
assert not response.headers.get("Cache-Control")
1053+
1054+
response = app.get(
1055+
f"/searches/{search_no_bbox}/point/-85.5,36.1624", params={"assets": "cog"}
1056+
)
1057+
assert response.status_code == 200
1058+
assert response.headers.get("Cache-Control")

titiler/pgstac/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ async def lifespan(app: FastAPI):
101101
allow_headers=["*"],
102102
)
103103

104-
app.add_middleware(CacheControlMiddleware, cachecontrol=settings.cachecontrol)
104+
app.add_middleware(
105+
CacheControlMiddleware,
106+
cachecontrol=settings.cachecontrol,
107+
exclude_path=settings.cachecontrol_exclude_paths,
108+
)
105109

106110
optional_headers = []
107111
if settings.debug:

titiler/pgstac/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""API settings."""
22

33
from functools import lru_cache
4-
from typing import Any, Optional
4+
from typing import Any, Optional, Set
55

66
from pydantic import (
77
Field,
@@ -20,6 +20,11 @@ class ApiSettings(BaseSettings):
2020
name: str = "titiler-pgstac"
2121
cors_origins: str = "*"
2222
cachecontrol: str = "public, max-age=3600"
23+
cachecontrol_exclude_paths: Set[str] = Field(
24+
default={
25+
".+/list",
26+
}
27+
)
2328
root_path: str = ""
2429
debug: bool = False
2530

0 commit comments

Comments
 (0)