Files
template-fastapi/{{project_slug}}/handlers/system.py.jinja
Aleksei Sokol 53f14a8624
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 1m40s
Version 0.4.0
Changes:
- put ObservabilityMiddleware before ExceptionHandlerMiddleware to avoid repetative code
- add application startup and last metrics update metrics along with CPU usage metric and threads count
- move host and port to new uvicorn section at config along with new reload and forwarded_allow_ips
- add request_id and remove trace_id/span_id generation if tracing is disabled
- move logging logic from utils to observability
- pass trace_id/span_id in HEX form
2026-01-03 16:29:58 +03:00

45 lines
1.4 KiB
Django/Jinja

"""System endpoints are defined here."""
import fastapi
from starlette import status
from {{project_slug}}.db.connection.manager import PostgresConnectionManager
from {{project_slug}}.dependencies import connection_manager_dep
from {{project_slug}}.logic import system as system_logic
from {{project_slug}}.schemas import PingResponse
from .routers import system_router
@system_router.get("/", status_code=status.HTTP_307_TEMPORARY_REDIRECT, include_in_schema=False)
@system_router.get("/api", status_code=status.HTTP_307_TEMPORARY_REDIRECT, include_in_schema=False)
async def redirect_to_swagger_docs():
"""Redirects to **/api/docs** from **/** and **/api**."""
return fastapi.responses.RedirectResponse("/api/docs", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
@system_router.get(
"/health_check/ping",
response_model=PingResponse,
status_code=status.HTTP_200_OK,
)
async def ping():
"""
Check that application is alive.
"""
return PingResponse()
@system_router.get(
"/health_check/ping_db",
response_model=PingResponse,
status_code=status.HTTP_200_OK,
)
async def ping_db(
readonly: bool = False,
connection_manager: PostgresConnectionManager = fastapi.Depends(connection_manager_dep.from_request),
):
"""Check that database connection is valid."""
return await system_logic.ping_db(connection_manager, readonly)