"""FastAPI application - LeoCRM backend.""" from __future__ import annotations from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.config import get_settings from app.core.middleware import CSRFMiddleware from app.core.db import close_engine from app.routes import auth, users, roles, tenants, health, notifications, companies @asynccontextmanager async def lifespan(app: FastAPI): """Application lifespan: startup and shutdown.""" yield await close_engine() def create_app() -> FastAPI: """Create and configure the FastAPI application.""" settings = get_settings() app = FastAPI(title="LeoCRM", version="1.0.0", lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=settings.cors_origin_list, allow_credentials=True, allow_methods=["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"], allow_headers=["Authorization", "Content-Type", "X-CSRF-Token", "X-Tenant-ID"], max_age=3600, ) app.add_middleware(CSRFMiddleware) app.include_router(health.router) app.include_router(auth.router) app.include_router(users.router) app.include_router(roles.router) app.include_router(tenants.router) app.include_router(notifications.router) app.include_router(companies.router) return app app = create_app()