|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import os |
| 9 | +from contextlib import asynccontextmanager |
9 | 10 |
|
10 | 11 | from brotli_asgi import BrotliMiddleware |
| 12 | +from fastapi import FastAPI |
11 | 13 | from fastapi.responses import ORJSONResponse |
12 | 14 | from stac_fastapi.api.app import StacApi |
13 | 15 | from stac_fastapi.api.middleware import CORSMiddleware, ProxyHeaderMiddleware |
|
18 | 20 | create_post_request_model, |
19 | 21 | create_request_model, |
20 | 22 | ) |
| 23 | +from stac_fastapi.api.openapi import update_openapi |
21 | 24 | from stac_fastapi.extensions.core import ( |
22 | 25 | FieldsExtension, |
23 | 26 | FilterExtension, |
24 | 27 | FreeTextExtension, |
| 28 | + OffsetPaginationExtension, |
25 | 29 | SortExtension, |
26 | 30 | TokenPaginationExtension, |
27 | 31 | TransactionExtension, |
|
61 | 65 | "fields": FieldsExtension(), |
62 | 66 | "filter": FilterExtension(client=FiltersClient()), |
63 | 67 | "free_text": FreeTextExtension(), |
| 68 | + "pagination": OffsetPaginationExtension(), |
64 | 69 | } |
65 | 70 |
|
66 | 71 | enabled_extensions = ( |
|
102 | 107 | post_request_model = create_post_request_model(extensions, base_model=PgstacSearch) |
103 | 108 | get_request_model = create_get_request_model(extensions) |
104 | 109 |
|
| 110 | + |
| 111 | +@asynccontextmanager |
| 112 | +async def lifespan(app: FastAPI): |
| 113 | + """FastAPI Lifespan.""" |
| 114 | + await connect_to_db(app) |
| 115 | + yield |
| 116 | + await close_db_connection(app) |
| 117 | + |
| 118 | + |
| 119 | +fastapp = FastAPI( |
| 120 | + openapi_url=settings.openapi_url, |
| 121 | + docs_url=settings.docs_url, |
| 122 | + redoc_url=None, |
| 123 | + root_path=settings.root_path, |
| 124 | + lifespan=lifespan, |
| 125 | +) |
| 126 | + |
| 127 | + |
105 | 128 | api = StacApi( |
| 129 | + app=update_openapi(fastapp), |
106 | 130 | settings=settings, |
107 | 131 | extensions=extensions + [collection_search_extension] |
108 | 132 | if collection_search_extension |
109 | 133 | else extensions, |
110 | | - client=CoreCrudClient(post_request_model=post_request_model), # type: ignore |
| 134 | + client=CoreCrudClient(pgstac_search_model=post_request_model), |
111 | 135 | response_class=ORJSONResponse, |
112 | 136 | items_get_request_model=items_get_request_model, |
113 | 137 | search_get_request_model=get_request_model, |
|
126 | 150 | app = api.app |
127 | 151 |
|
128 | 152 |
|
129 | | -@app.on_event("startup") |
130 | | -async def startup_event(): |
131 | | - """Connect to database on startup.""" |
132 | | - await connect_to_db(app) |
133 | | - |
134 | | - |
135 | | -@app.on_event("shutdown") |
136 | | -async def shutdown_event(): |
137 | | - """Close database connection.""" |
138 | | - await close_db_connection(app) |
139 | | - |
140 | | - |
141 | 153 | def run(): |
142 | 154 | """Run app from command line using uvicorn if available.""" |
143 | 155 | try: |
|
0 commit comments