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
|
from contextlib import asynccontextmanager
from fastapi import FastAPI
from pymongo import AsyncMongoClient
from beanie import init_beanie
from tests.conftest import Settings
from tests.fastapi.models import (
DoorAPI,
House,
HouseAPI,
Person,
RoofAPI,
WindowAPI,
)
from tests.fastapi.routes import house_router
@asynccontextmanager
async def live_span(_: FastAPI):
# CREATE ASYNC PYMONGO CLIENT
client = AsyncMongoClient(Settings().mongodb_dsn)
# INIT BEANIE
await init_beanie(
client.beanie_db,
document_models=[House, Person, HouseAPI, WindowAPI, DoorAPI, RoofAPI],
)
yield
app = FastAPI(lifespan=live_span)
# ADD ROUTES
app.include_router(house_router, prefix="/v1", tags=["house"])
|