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