/
main.py
52 lines (39 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
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
"""The main application factory for the example service.
Notes
-----
Be aware that, following the normal pattern for FastAPI services, the app is
constructed when this module is loaded and is not deferred until a function is
called.
"""
from importlib.metadata import metadata, version
from fastapi import FastAPI
from safir.dependencies.http_client import http_client_dependency
from safir.logging import configure_logging
from safir.middleware.x_forwarded import XForwardedMiddleware
from .config import config
from .handlers.external import external_router
from .handlers.internal import internal_router
__all__ = ["app", "config"]
configure_logging(
profile=config.profile,
log_level=config.log_level,
name=config.logger_name,
)
app = FastAPI(
title="example",
description=metadata("example")["Summary"],
version=version("example"),
openapi_url=f"/{config.name}/openapi.json",
docs_url=f"/{config.name}/docs",
redoc_url=f"/{config.name}/redoc",
)
"""The main FastAPI application for example."""
# Attach the routers.
app.include_router(internal_router)
app.include_router(external_router, prefix=f"/{config.name}")
@app.on_event("startup")
async def startup_event() -> None:
app.add_middleware(XForwardedMiddleware)
@app.on_event("shutdown")
async def shutdown_event() -> None:
await http_client_dependency.aclose()