martian7777
feat: implement backend core with ORM models, authentication, and AI-driven telemetry diagnostics
e5be436 | """FastAPI application factory: middleware, routers, lifespan, error handling.""" | |
| from __future__ import annotations | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from app.api.v1 import api_router | |
| from app.core.config import settings | |
| from app.core.exceptions import register_exception_handlers | |
| from app.core.logging import configure_logging, get_logger | |
| from app.schemas.common import HealthResponse | |
| configure_logging() | |
| logger = get_logger(__name__) | |
| async def lifespan(app: FastAPI): | |
| logger.info( | |
| "startup", | |
| app=settings.app_name, | |
| env=settings.environment, | |
| ai_enabled=settings.ai_enabled, | |
| ) | |
| yield | |
| logger.info("shutdown") | |
| def create_app() -> FastAPI: | |
| app = FastAPI( | |
| title=settings.app_name, | |
| version="1.0.0", | |
| description="Production-grade predictive maintenance API.", | |
| lifespan=lifespan, | |
| docs_url="/docs", | |
| redoc_url="/redoc", | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # tighten for production deployments | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| register_exception_handlers(app) | |
| app.include_router(api_router, prefix=settings.api_v1_prefix) | |
| async def health() -> HealthResponse: | |
| return HealthResponse( | |
| status="ok", | |
| environment=settings.environment, | |
| ai_enabled=settings.ai_enabled, | |
| ) | |
| async def root() -> dict[str, str]: | |
| return { | |
| "name": settings.app_name, | |
| "docs": "/docs", | |
| "health": "/health", | |
| } | |
| return app | |
| app = create_app() | |