Spaces:
Running on Zero
Running on Zero
Download app/main.py from avijit28/tunechecker-api: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/avijit28/tunechecker-api/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/avijit28/tunechecker-api/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/avijit28/tunechecker-api/resolve/main/app/main.py
2.96 kB
| import uuid | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.exceptions import RequestValidationError | |
| from starlette.exceptions import HTTPException as StarletteHTTPException | |
| from app.config import settings | |
| from app.logging_config import setup_logging, request_id_ctx | |
| from app.utils.errors import ( | |
| AppError, | |
| app_error_handler, | |
| validation_error_handler, | |
| http_error_handler, | |
| generic_error_handler | |
| ) | |
| from app.api.v1.router import api_router | |
| from app.core.middleware import SecurityHeadersMiddleware | |
| from app.core.rate_limiter import RateLimiterMiddleware | |
| from app.db.enterprise import init_enterprise_databases, close_enterprise_databases | |
| setup_logging(settings.LOG_LEVEL) | |
| async def lifespan(app: FastAPI): | |
| # Startup: Initialize all 6 enterprise database adapters in parallel | |
| await init_enterprise_databases() | |
| yield | |
| # Shutdown: Safely close database connection pools | |
| await close_enterprise_databases() | |
| def create_app() -> FastAPI: | |
| app = FastAPI( | |
| title="TuneChecker API", | |
| description="Sales Call Quality Analysis Agent API", | |
| version="1.0.0", | |
| docs_url="/api/docs", | |
| openapi_url="/api/openapi.json", | |
| lifespan=lifespan, | |
| ) | |
| # Rate Limiter Middleware (<2ms overhead, sliding-window Redis + fallback) | |
| app.add_middleware(RateLimiterMiddleware) | |
| # Enterprise Security Headers Middleware (HSTS, CSP, nosniff, DENY) | |
| app.add_middleware(SecurityHeadersMiddleware) | |
| # CORS | |
| origins = [o.strip() for o in settings.CORS_ORIGINS.split(",") if o.strip()] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| expose_headers=[ | |
| "X-Request-ID", | |
| "X-RateLimit-Limit", | |
| "X-RateLimit-Remaining", | |
| "X-RateLimit-Reset", | |
| "X-RateLimit-Overhead-Ms", | |
| "Retry-After", | |
| ] | |
| ) | |
| # Request ID Middleware | |
| async def request_id_middleware(request: Request, call_next): | |
| req_id = request.headers.get("X-Request-ID") or f"req_{uuid.uuid4().hex[:8]}" | |
| token = request_id_ctx.set(req_id) | |
| try: | |
| response = await call_next(request) | |
| response.headers["X-Request-ID"] = req_id | |
| return response | |
| finally: | |
| request_id_ctx.reset(token) | |
| # Register Exception Handlers | |
| app.add_exception_handler(AppError, app_error_handler) | |
| app.add_exception_handler(RequestValidationError, validation_error_handler) | |
| app.add_exception_handler(StarletteHTTPException, http_error_handler) | |
| app.add_exception_handler(Exception, generic_error_handler) | |
| # Include API Routers | |
| app.include_router(api_router, prefix="/api/v1") | |
| return app | |
| app = create_app() | |