Skip to content

Commit d6508cb

Browse files
committed
refactor / polish / docs / examples / changelog
1 parent 0f402e5 commit d6508cb

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111

1212
- Added `datetime_frequency_interval` parameter for `datetime_frequency` aggregation. [#294](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/294)
13+
- Added rate limiting functionality with configurable limits using environment variable `STAC_FASTAPI_RATE_LIMIT`, example: `500/minute`. [#303](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/303)
1314

1415
### Changed
1516

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,8 @@ Available aggregations are:
383383
- geometry_geohash_grid_frequency ([geohash grid](https://opensearch.org/docs/latest/aggregations/bucket/geohash-grid/) on Item.geometry)
384384
- geometry_geotile_grid_frequency ([geotile grid](https://opensearch.org/docs/latest/aggregations/bucket/geotile-grid/) on Item.geometry)
385385

386-
Support for additional fields and new aggregations can be added in the associated `database_logic.py` file.
386+
Support for additional fields and new aggregations can be added in the associated `database_logic.py` file.
387+
388+
## Rate Limiting
389+
390+
Rate limiting is an optional feature that can be enabled through the `STAC_FASTAPI_RATE_LIMIT` environment variable (example value: `500/minute`). Examples can be found and a more detailed explanation is provided in [examples/rate_limit](examples/rate_limit).

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ services:
2222
- ES_USE_SSL=false
2323
- ES_VERIFY_CERTS=false
2424
- BACKEND=elasticsearch
25-
- STAC_FASTAPI_RATE_LIMIT=500/minute
2625
ports:
2726
- "8080:8080"
2827
volumes:
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
version: '3.9'
2+
3+
services:
4+
app-elasticsearch:
5+
container_name: stac-fastapi-es
6+
image: stac-utils/stac-fastapi-es
7+
restart: always
8+
build:
9+
context: .
10+
dockerfile: dockerfiles/Dockerfile.dev.es
11+
environment:
12+
- STAC_FASTAPI_TITLE=stac-fastapi-elasticsearch
13+
- STAC_FASTAPI_DESCRIPTION=A STAC FastAPI with an Elasticsearch backend
14+
- STAC_FASTAPI_VERSION=2.1
15+
- APP_HOST=0.0.0.0
16+
- APP_PORT=8080
17+
- RELOAD=true
18+
- ENVIRONMENT=local
19+
- WEB_CONCURRENCY=10
20+
- ES_HOST=elasticsearch
21+
- ES_PORT=9200
22+
- ES_USE_SSL=false
23+
- ES_VERIFY_CERTS=false
24+
- BACKEND=elasticsearch
25+
- STAC_FASTAPI_RATE_LIMIT=500/minute
26+
ports:
27+
- "8080:8080"
28+
volumes:
29+
- ./stac_fastapi:/app/stac_fastapi
30+
- ./scripts:/app/scripts
31+
- ./esdata:/usr/share/elasticsearch/data
32+
depends_on:
33+
- elasticsearch
34+
command:
35+
bash -c "./scripts/wait-for-it-es.sh es-container:9200 && python -m stac_fastapi.elasticsearch.app"
36+
37+
app-opensearch:
38+
container_name: stac-fastapi-os
39+
image: stac-utils/stac-fastapi-os
40+
restart: always
41+
build:
42+
context: .
43+
dockerfile: dockerfiles/Dockerfile.dev.os
44+
environment:
45+
- STAC_FASTAPI_TITLE=stac-fastapi-opensearch
46+
- STAC_FASTAPI_DESCRIPTION=A STAC FastAPI with an Opensearch backend
47+
- STAC_FASTAPI_VERSION=3.0.0a2
48+
- APP_HOST=0.0.0.0
49+
- APP_PORT=8082
50+
- RELOAD=true
51+
- ENVIRONMENT=local
52+
- WEB_CONCURRENCY=10
53+
- ES_HOST=opensearch
54+
- ES_PORT=9202
55+
- ES_USE_SSL=false
56+
- ES_VERIFY_CERTS=false
57+
- BACKEND=opensearch
58+
- STAC_FASTAPI_RATE_LIMIT=200/minute
59+
ports:
60+
- "8082:8082"
61+
volumes:
62+
- ./stac_fastapi:/app/stac_fastapi
63+
- ./scripts:/app/scripts
64+
- ./osdata:/usr/share/opensearch/data
65+
depends_on:
66+
- opensearch
67+
command:
68+
bash -c "./scripts/wait-for-it-es.sh os-container:9202 && python -m stac_fastapi.opensearch.app"
69+
70+
elasticsearch:
71+
container_name: es-container
72+
image: docker.elastic.co/elasticsearch/elasticsearch:${ELASTICSEARCH_VERSION:-8.11.0}
73+
hostname: elasticsearch
74+
environment:
75+
ES_JAVA_OPTS: -Xms512m -Xmx1g
76+
volumes:
77+
- ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
78+
- ./elasticsearch/snapshots:/usr/share/elasticsearch/snapshots
79+
ports:
80+
- "9200:9200"
81+
82+
opensearch:
83+
container_name: os-container
84+
image: opensearchproject/opensearch:${OPENSEARCH_VERSION:-2.11.1}
85+
hostname: opensearch
86+
environment:
87+
- discovery.type=single-node
88+
- plugins.security.disabled=true
89+
- OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m
90+
volumes:
91+
- ./opensearch/config/opensearch.yml:/usr/share/opensearch/config/opensearch.yml
92+
- ./opensearch/snapshots:/usr/share/opensearch/snapshots
93+
ports:
94+
- "9202:9202"

stac_fastapi/core/stac_fastapi/core/rate_limit.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@
1212

1313
logger = logging.getLogger(__name__)
1414

15+
1516
def get_limiter(key_func=get_remote_address):
17+
"""Create and return a Limiter instance for rate limiting."""
1618
return Limiter(key_func=key_func)
1719

20+
1821
def setup_rate_limit(
19-
app: FastAPI,
20-
rate_limit: Optional[str] = None,
21-
key_func=get_remote_address
22+
app: FastAPI, rate_limit: Optional[str] = None, key_func=get_remote_address
2223
):
2324
"""Set up rate limiting middleware."""
2425
RATE_LIMIT = rate_limit or os.getenv("STAC_FASTAPI_RATE_LIMIT")
25-
26+
2627
if not RATE_LIMIT:
2728
logger.info("Rate limiting is disabled")
2829
return
2930

3031
logger.info(f"Setting up rate limit with RATE_LIMIT={RATE_LIMIT}")
31-
32+
3233
limiter = get_limiter(key_func)
3334
app.state.limiter = limiter
3435
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

0 commit comments

Comments
 (0)