Skip to content
Merged
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
34 changes: 22 additions & 12 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"""

import os
from contextlib import asynccontextmanager

from brotli_asgi import BrotliMiddleware
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse
from stac_fastapi.api.app import StacApi
from stac_fastapi.api.middleware import CORSMiddleware, ProxyHeaderMiddleware
Expand All @@ -18,6 +20,7 @@
create_post_request_model,
create_request_model,
)
from stac_fastapi.api.openapi import update_openapi
from stac_fastapi.extensions.core import (
FieldsExtension,
FilterExtension,
Expand Down Expand Up @@ -101,7 +104,26 @@
post_request_model = create_post_request_model(extensions, base_model=PgstacSearch)
get_request_model = create_get_request_model(extensions)


@asynccontextmanager
async def lifespan(app: FastAPI):
"""FastAPI Lifespan."""
await connect_to_db(app)
yield
await close_db_connection(app)


fastapp = FastAPI(
openapi_url=settings.openapi_url,
docs_url=settings.docs_url,
redoc_url=None,
root_path=getattr(settings, "root_path", None),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root_path settings was introduced in 3.0.4

lifespan=lifespan,
)


api = StacApi(
app=update_openapi(fastapp),
settings=settings,
extensions=extensions + [collection_search_extension]
if collection_search_extension
Expand All @@ -125,18 +147,6 @@
app = api.app


@app.on_event("startup")
async def startup_event():
"""Connect to database on startup."""
await connect_to_db(app)


@app.on_event("shutdown")
async def shutdown_event():
"""Close database connection."""
await close_db_connection(app)


def run():
"""Run app from command line using uvicorn if available."""
try:
Expand Down
Loading