/
rinter
/
fastapi-demo
Обзор
Документация
Войти
/
rinter
/
fastapi-demo
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
45 строк
935 B
rinter
feat: add healthcheck
14 ноя 2025, 10:47
14 ноя 2025, 10:47
6fa2176
Код
Авторство
О чём код?
from typing import Optional, AsyncGenerator from fastapi import FastAPI from prisma import Prisma from pydantic import BaseModel db = Prisma() class CreatePostDto(BaseModel): title: str content: Optional[str] = None published: bool = False async def lifespan(app: FastAPI) -> AsyncGenerator: await db.connect() yield await db.disconnect() # Передаём lifespan в FastAPI app = FastAPI(lifespan=lifespan) @app.get("/") async def list_posts(): return await db.post.find_many() @app.post("/") async def create_post(dto: CreatePostDto): return await db.post.create(data=dto.model_dump(exclude_none=True)) @app.get("/health") async def health_check(): health = {"app": "ok", "database": "unknown"} try: await db.execute_raw("SELECT 1;") health["database"] = "ok" except Exception as e: health["database"] = f"unreachable: {str(e)}" return health