Skip to content

Lifespan context #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""FastAPI application."""

import os
from contextlib import asynccontextmanager

from fastapi import FastAPI

from stac_fastapi.api.app import StacApi
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
Expand Down Expand Up @@ -97,17 +100,21 @@
search_post_request_model=post_request_model,
route_dependencies=get_route_dependencies(),
)
app = api.app
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")

# Add rate limit
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))


@app.on_event("startup")
async def _startup_event() -> None:
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan handler for FastAPI app. Initializes index templates and collections at startup."""
await create_index_templates()
await create_collection_index()
yield


app = api.app
app.router.lifespan_context = lifespan
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
# Add rate limit
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))


def run() -> None:
Expand Down
22 changes: 14 additions & 8 deletions stac_fastapi/opensearch/stac_fastapi/opensearch/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""FastAPI application."""

import os
from contextlib import asynccontextmanager

from fastapi import FastAPI

from stac_fastapi.api.app import StacApi
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
Expand Down Expand Up @@ -97,18 +100,21 @@
search_post_request_model=post_request_model,
route_dependencies=get_route_dependencies(),
)
app = api.app
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")


# Add rate limit
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))


@app.on_event("startup")
async def _startup_event() -> None:
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan handler for FastAPI app. Initializes index templates and collections at startup."""
await create_index_templates()
await create_collection_index()
yield


app = api.app
app.router.lifespan_context = lifespan
app.root_path = os.getenv("STAC_FASTAPI_ROOT_PATH", "")
# Add rate limit
setup_rate_limit(app, rate_limit=os.getenv("STAC_FASTAPI_RATE_LIMIT"))


def run() -> None:
Expand Down