Builds web application
:return: FastAPI instance that serves the API
Source code in app/__init__.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 | def create_app() -> "FastAPI":
"""
Builds web application
:return: FastAPI instance that serves the API
"""
from app.api import router
from app.core.config import settings
from app.core.globals import lazy_globals
from app.middleware.access_logging import AccessLoggingMiddleware
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
"""
Lifespan hook is called on app initialization, this hook:
- configures logging
- enables metrics
- connects to the database
- initiates caching
Code after `yield` is executed on the app shutdown
:param _app: instance of the application
"""
uvicorn_access_logger = logging.getLogger("uvicorn.access")
uvicorn_access_logger.setLevel("WARN")
prisma = Prisma(auto_register=True)
await prisma.connect()
yield
await prisma.disconnect()
app = FastAPI(
lifespan=lifespan,
title=settings.PROJECT_NAME,
openapi_url=f"{settings.BASE_URL}/openapi.json",
version=settings.VERSION,
redoc_url=f"{settings.BASE_URL}/redoc",
docs_url=f"{settings.BASE_URL}/docs",
)
@app.get("/ping", include_in_schema=False)
async def ping() -> str:
return "pong"
app.add_middleware(
CORSMiddleware,
allow_origins=settings.BACKEND_CORS_ORIGINS,
allow_origin_regex=settings.BACKEND_CORS_ORIGINS_REGEX,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(
AccessLoggingMiddleware,
context=context,
paths_to_ignore={"/ping", "/metrics"},
)
app.add_middleware(RawContextMiddleware, plugins=[plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin()])
app.include_router(router)
simplify_operation_ids(app)
return app
|