from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.core.config import settings from app.core.database import engine, Base from app.api.v1.router import api_router # --- Lifespan Manager (Startup & Shutdown Logic) --- @asynccontextmanager async def lifespan(app: FastAPI): # STARTUP: Adatbázis táblák létrehozása # Figyelem: Éles környezetben (Production) inkább Alembic migrációt használunk! async with engine.begin() as conn: # Betöltjük a modelleket, hogy a metadata tudjon róluk # Fontos: importálni kell őket, különben nem jönnek létre! from app.models import social, vehicle, user, logistics await conn.run_sync(Base.metadata.create_all) print("--- Database Tables Checked/Created ---") yield # Itt fut az alkalmazás... # SHUTDOWN: Erőforrások felszabadítása (ha lenne) print("--- Shutting down ---") # --- App Initialization --- app = FastAPI( title=settings.PROJECT_NAME, version=settings.VERSION, openapi_url=f"{settings.API_V1_STR}/openapi.json", lifespan=lifespan ) # --- Middleware (CORS) --- # Engedélyezzük a frontend kommunikációt app.add_middleware( CORSMiddleware, allow_origins=["*"], # Élesben szigorítani kell! allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # --- Routers --- app.include_router(api_router, prefix=settings.API_V1_STR) @app.get("/") async def root(): return { "message": "Traffic Ecosystem SuperApp API is Running", "docs_url": "/docs", "version": settings.VERSION }