Skip to content

Commit 2fd11fe

Browse files
committed
use lifespan context manager
1 parent 1e7346f commit 2fd11fe

File tree

2 files changed

+28
-15
lines changed
  • stac_fastapi
    • elasticsearch/stac_fastapi/elasticsearch
    • opensearch/stac_fastapi/opensearch

2 files changed

+28
-15
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""FastAPI application."""
22

33
import os
4+
from contextlib import asynccontextmanager
5+
6+
from fastapi import FastAPI
47

58
from stac_fastapi.api.app import StacApi
69
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
@@ -97,17 +100,21 @@
97100
search_post_request_model=post_request_model,
98101
route_dependencies=get_route_dependencies(),
99102
)
100-
app = api.app
101-
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
102-
103-
# Add rate limit
104-
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))
105103

106104

107-
@app.on_event("startup")
108-
async def _startup_event() -> None:
105+
@asynccontextmanager
106+
async def lifespan(app: FastAPI):
107+
"""Lifespan handler for FastAPI app. Initializes index templates and collections at startup."""
109108
await create_index_templates()
110109
await create_collection_index()
110+
yield
111+
112+
113+
app = api.app
114+
app.router.lifespan_context = lifespan
115+
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
116+
# Add rate limit
117+
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))
111118

112119

113120
def run() -> None:

stac_fastapi/opensearch/stac_fastapi/opensearch/app.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""FastAPI application."""
22

33
import os
4+
from contextlib import asynccontextmanager
5+
6+
from fastapi import FastAPI
47

58
from stac_fastapi.api.app import StacApi
69
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
@@ -97,18 +100,21 @@
97100
search_post_request_model=post_request_model,
98101
route_dependencies=get_route_dependencies(),
99102
)
100-
app = api.app
101-
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
102-
103-
104-
# Add rate limit
105-
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))
106103

107104

108-
@app.on_event("startup")
109-
async def _startup_event() -> None:
105+
@asynccontextmanager
106+
async def lifespan(app: FastAPI):
107+
"""Lifespan handler for FastAPI app. Initializes index templates and collections at startup."""
110108
await create_index_templates()
111109
await create_collection_index()
110+
yield
111+
112+
113+
app = api.app
114+
app.router.lifespan_context = lifespan
115+
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
116+
# Add rate limit
117+
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))
112118

113119

114120
def run() -> None:

0 commit comments

Comments
 (0)