80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
"""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.db import close_engine, get_engine
|
|
from app.core.middleware import CSRFMiddleware
|
|
from app.core.service_container import get_container
|
|
from app.plugins.registry import get_registry
|
|
from app.routes import (
|
|
ai_copilot,
|
|
auth,
|
|
companies,
|
|
contacts,
|
|
health,
|
|
import_export,
|
|
notifications,
|
|
plugins,
|
|
roles,
|
|
tenants,
|
|
users,
|
|
workflows,
|
|
)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""Application lifespan: startup and shutdown."""
|
|
# Initialize service container
|
|
container = get_container()
|
|
await container.initialize()
|
|
|
|
# Initialize plugin registry and discover built-in plugins
|
|
registry = get_registry()
|
|
registry.initialize(get_engine(), app)
|
|
registry.discover_builtins()
|
|
|
|
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)
|
|
app.include_router(contacts.router)
|
|
app.include_router(import_export.router)
|
|
app.include_router(plugins.router)
|
|
app.include_router(ai_copilot.router)
|
|
app.include_router(workflows.router)
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|