|
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,
|
|
101 | 104 | post_request_model = create_post_request_model(extensions, base_model=PgstacSearch)
|
102 | 105 | get_request_model = create_get_request_model(extensions)
|
103 | 106 |
|
| 107 | + |
| 108 | +@asynccontextmanager |
| 109 | +async def lifespan(app: FastAPI): |
| 110 | + """FastAPI Lifespan.""" |
| 111 | + await connect_to_db(app) |
| 112 | + yield |
| 113 | + await close_db_connection(app) |
| 114 | + |
| 115 | + |
| 116 | +fastapp = FastAPI( |
| 117 | + openapi_url=settings.openapi_url, |
| 118 | + docs_url=settings.docs_url, |
| 119 | + redoc_url=None, |
| 120 | + root_path=getattr(settings, "root_path", None), |
| 121 | + lifespan=lifespan, |
| 122 | +) |
| 123 | + |
| 124 | + |
104 | 125 | api = StacApi(
|
| 126 | + app=update_openapi(fastapp), |
105 | 127 | settings=settings,
|
106 | 128 | extensions=extensions + [collection_search_extension]
|
107 | 129 | if collection_search_extension
|
|
125 | 147 | app = api.app
|
126 | 148 |
|
127 | 149 |
|
128 |
| -@app.on_event("startup") |
129 |
| -async def startup_event(): |
130 |
| - """Connect to database on startup.""" |
131 |
| - await connect_to_db(app) |
132 |
| - |
133 |
| - |
134 |
| -@app.on_event("shutdown") |
135 |
| -async def shutdown_event(): |
136 |
| - """Close database connection.""" |
137 |
| - await close_db_connection(app) |
138 |
| - |
139 |
| - |
140 | 150 | def run():
|
141 | 151 | """Run app from command line using uvicorn if available."""
|
142 | 152 | try:
|
|
0 commit comments