import asyncio import logging from contextlib import asynccontextmanager from typing import Callable, cast from dotenv import load_dotenv load_dotenv() import stripe from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from slowapi.errors import RateLimitExceeded from slowapi import _rate_limit_exceeded_handler from core.config import CORS_ORIGINS, STRIPE_SECRET_KEY from core.dependencies import limiter from services.database_functions import fill_card_pool, run_cleanup_loop from routers import auth, cards, decks, games, health, notifications, profile, friends, store, trades stripe.api_key = STRIPE_SECRET_KEY logger = logging.getLogger("app") @asynccontextmanager async def lifespan(app: FastAPI): asyncio.create_task(fill_card_pool()) asyncio.create_task(run_cleanup_loop()) yield app = FastAPI(lifespan=lifespan) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, cast(Callable, _rate_limit_exceeded_handler)) app.add_middleware( CORSMiddleware, allow_origins=CORS_ORIGINS, allow_methods=["*"], allow_headers=["*"], ) app.include_router(health.router) app.include_router(auth.router) app.include_router(cards.router) app.include_router(decks.router) app.include_router(games.router) app.include_router(notifications.router) app.include_router(profile.router) app.include_router(friends.router) app.include_router(store.router) app.include_router(trades.router)