7a7daf8100
- 10 models: tenants, users, user_tenants, roles, sessions, audit_log, deletion_log, notifications, password_reset_tokens, api_tokens - Session-based auth (Redis + PostgreSQL audit trail) - Multi-tenant with ORM-level filtering + PostgreSQL RLS (set_config) - RBAC with roles/permissions + field-level permissions - CSRF protection via Origin header validation - Auth rate limiting (Redis counters with TTL) - CORS with explicit origins (no wildcard) - Health endpoint (no auth required) - Notification service + audit log middleware - 29 tests, 26 ACs, all passing - Coverage: 62% (infrastructure modules pending coverage in later tasks)
50 lines
1.4 KiB
Python
50 lines
1.4 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.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()
|