|
1 | 1 | """TiTiler+PgSTAC FastAPI application.""" |
2 | 2 |
|
3 | 3 | import logging |
| 4 | +from contextlib import asynccontextmanager |
4 | 5 | from typing import Dict |
5 | 6 |
|
6 | 7 | import pystac |
|
17 | 18 | from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers |
18 | 19 | from titiler.core.factory import AlgorithmFactory, MultiBaseTilerFactory, TMSFactory |
19 | 20 | from titiler.core.middleware import CacheControlMiddleware |
20 | | -from titiler.core.resources.enums import OptionalHeader |
21 | 21 | from titiler.mosaic.errors import MOSAIC_STATUS_CODES |
22 | 22 | from titiler.pgstac.db import close_db_connection, connect_to_db |
23 | 23 | from titiler.pgstac.dependencies import ItemPathParams |
|
40 | 40 | templates = Jinja2Templates(directory=str(resources_files(__package__) / "templates")) # type: ignore |
41 | 41 |
|
42 | 42 |
|
43 | | -if settings.debug: |
44 | | - optional_headers = [OptionalHeader.server_timing, OptionalHeader.x_assets] |
45 | | -else: |
46 | | - optional_headers = [] |
| 43 | +@asynccontextmanager |
| 44 | +async def lifespan(app: FastAPI): |
| 45 | + """FastAPI Lifespan.""" |
| 46 | + # Create Connection Pool |
| 47 | + await connect_to_db(app) |
| 48 | + yield |
| 49 | + # Close the Connection Pool |
| 50 | + await close_db_connection(app) |
| 51 | + |
47 | 52 |
|
48 | | -app = FastAPI(title=settings.name, version=eoapi_raster_version) |
| 53 | +app = FastAPI( |
| 54 | + title=settings.name, |
| 55 | + version=eoapi_raster_version, |
| 56 | + root_path=settings.root_path, |
| 57 | + lifespan=lifespan, |
| 58 | +) |
49 | 59 | add_exception_handlers(app, DEFAULT_STATUS_CODES) |
50 | 60 | add_exception_handlers(app, MOSAIC_STATUS_CODES) |
51 | 61 |
|
| 62 | +if settings.cors_origins: |
| 63 | + app.add_middleware( |
| 64 | + CORSMiddleware, |
| 65 | + allow_origins=settings.cors_origins, |
| 66 | + allow_credentials=True, |
| 67 | + allow_methods=["GET", "POST", "OPTIONS"], |
| 68 | + allow_headers=["*"], |
| 69 | + ) |
| 70 | + |
| 71 | +app.add_middleware( |
| 72 | + CacheControlMiddleware, |
| 73 | + cachecontrol=settings.cachecontrol, |
| 74 | + exclude_path={r"/healthz"}, |
| 75 | +) |
| 76 | +app.add_middleware( |
| 77 | + CompressionMiddleware, |
| 78 | + exclude_mediatype={ |
| 79 | + "image/jpeg", |
| 80 | + "image/jpg", |
| 81 | + "image/png", |
| 82 | + "image/jp2", |
| 83 | + "image/webp", |
| 84 | + }, |
| 85 | +) |
| 86 | + |
52 | 87 | ############################################################################### |
53 | 88 | # MOSAIC Endpoints |
54 | 89 | mosaic = MosaicTilerFactory( |
55 | | - optional_headers=optional_headers, |
56 | 90 | router_prefix="/mosaic", |
57 | 91 | add_statistics=True, |
58 | 92 | # add /map viewer |
@@ -93,7 +127,6 @@ async def list_collection(request: Request): |
93 | 127 | stac = MultiBaseTilerFactory( |
94 | 128 | reader=PgSTACReader, |
95 | 129 | path_dependency=ItemPathParams, |
96 | | - optional_headers=optional_headers, |
97 | 130 | router_prefix="/collections/{collection_id}/items/{item_id}", |
98 | 131 | # add /map viewer |
99 | 132 | add_viewer=True, |
@@ -178,25 +211,37 @@ def landing(request: Request): |
178 | 211 | "rel": "service-doc", |
179 | 212 | }, |
180 | 213 | { |
181 | | - "title": "Mosaic List (JSON)", |
| 214 | + "title": "STAC Item Asset's Info (template URL)", |
| 215 | + "href": stac.url_for(request, "info"), |
| 216 | + "type": "application/json", |
| 217 | + "rel": "data", |
| 218 | + }, |
| 219 | + { |
| 220 | + "title": "STAC Item Viewer (template URL)", |
| 221 | + "href": stac.url_for(request, "viewer"), |
| 222 | + "type": "text/html", |
| 223 | + "rel": "data", |
| 224 | + }, |
| 225 | + { |
| 226 | + "title": "STAC Mosaic List (JSON)", |
182 | 227 | "href": mosaic.url_for(request, "list_mosaic"), |
183 | 228 | "type": "application/json", |
184 | 229 | "rel": "data", |
185 | 230 | }, |
186 | 231 | { |
187 | | - "title": "Mosaic Builder", |
| 232 | + "title": "STAC Mosaic Builder", |
188 | 233 | "href": mosaic.url_for(request, "mosaic_builder"), |
189 | 234 | "type": "text/html", |
190 | 235 | "rel": "data", |
191 | 236 | }, |
192 | 237 | { |
193 | | - "title": "Mosaic Metadata (template URL)", |
| 238 | + "title": "STAC Mosaic Metadata (template URL)", |
194 | 239 | "href": mosaic.url_for(request, "info_search", searchid="{searchid}"), |
195 | 240 | "type": "application/json", |
196 | 241 | "rel": "data", |
197 | 242 | }, |
198 | 243 | { |
199 | | - "title": "Mosaic viewer (template URL)", |
| 244 | + "title": "STAC Mosaic viewer (template URL)", |
200 | 245 | "href": mosaic.url_for(request, "map_viewer", searchid="{searchid}"), |
201 | 246 | "type": "text/html", |
202 | 247 | "rel": "data", |
@@ -240,41 +285,3 @@ def landing(request: Request): |
240 | 285 | "urlparams": str(request.url.query), |
241 | 286 | }, |
242 | 287 | ) |
243 | | - |
244 | | - |
245 | | -if settings.cors_origins: |
246 | | - app.add_middleware( |
247 | | - CORSMiddleware, |
248 | | - allow_origins=settings.cors_origins, |
249 | | - allow_credentials=True, |
250 | | - allow_methods=["GET", "POST", "OPTIONS"], |
251 | | - allow_headers=["*"], |
252 | | - ) |
253 | | - |
254 | | -app.add_middleware( |
255 | | - CacheControlMiddleware, |
256 | | - cachecontrol=settings.cachecontrol, |
257 | | - exclude_path={r"/healthz"}, |
258 | | -) |
259 | | -app.add_middleware( |
260 | | - CompressionMiddleware, |
261 | | - exclude_mediatype={ |
262 | | - "image/jpeg", |
263 | | - "image/jpg", |
264 | | - "image/png", |
265 | | - "image/jp2", |
266 | | - "image/webp", |
267 | | - }, |
268 | | -) |
269 | | - |
270 | | - |
271 | | -@app.on_event("startup") |
272 | | -async def startup_event() -> None: |
273 | | - """Connect to database on startup.""" |
274 | | - await connect_to_db(app) |
275 | | - |
276 | | - |
277 | | -@app.on_event("shutdown") |
278 | | -async def shutdown_event() -> None: |
279 | | - """Close database connection.""" |
280 | | - await close_db_connection(app) |
|
0 commit comments