from __future__ import annotations from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from api.config import settings from api.routes import router as api_router from bot.db.database import Database from bot.db.repository import BudgetRepository from bot.services.budget import BudgetService @asynccontextmanager async def lifespan(app: FastAPI): db = Database(settings.database_path, read_only=False) await db.connect() app.state.db = db app.state.budget_service = BudgetService(BudgetRepository(db)) try: yield finally: await db.close() app = FastAPI( title="Please Pay Me API", version="1.0.0", lifespan=lifespan, ) app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origin_list, allow_credentials=True, allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"], allow_headers=["Authorization", "X-API-Token", "Content-Type"], ) @app.get("/api/health") async def health() -> dict[str, str]: return {"status": "ok"} app.include_router(api_router)