File tree Expand file tree Collapse file tree 2 files changed +28
-15
lines changed
elasticsearch/stac_fastapi/elasticsearch
opensearch/stac_fastapi/opensearch Expand file tree Collapse file tree 2 files changed +28
-15
lines changed Original file line number Diff line number Diff line change 11"""FastAPI application."""
22
33import os
4+ from contextlib import asynccontextmanager
5+
6+ from fastapi import FastAPI
47
58from stac_fastapi .api .app import StacApi
69from stac_fastapi .api .models import create_get_request_model , create_post_request_model
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
113120def run () -> None :
Original file line number Diff line number Diff line change 11"""FastAPI application."""
22
33import os
4+ from contextlib import asynccontextmanager
5+
6+ from fastapi import FastAPI
47
58from stac_fastapi .api .app import StacApi
69from stac_fastapi .api .models import create_get_request_model , create_post_request_model
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
114120def run () -> None :
You can’t perform that action at this time.
0 commit comments