Spaces:
Sleeping
Sleeping
Download app/main.py from MrTechie/audit-automation-api: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/MrTechie/audit-automation-api/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/MrTechie/audit-automation-api/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/MrTechie/audit-automation-api/resolve/main/app/main.py
2.57 kB
| import asyncio | |
| from collections.abc import AsyncIterator, Mapping | |
| from contextlib import asynccontextmanager | |
| from datetime import timedelta | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from app.domain.tool import Tool | |
| from app.logs import configure_logging | |
| from app.registry import REGISTRY | |
| from app.routes import errors, health, jobs, tools | |
| from app.routes.limits import BodyLimit | |
| from app.routes.origin import OriginGuard | |
| from app.services.jobs import InMemoryJobStore, Sweeper | |
| from app.services.runner import JobRunner | |
| from app.settings import Settings | |
| def create_app( | |
| settings: Settings | None = None, tools_by_id: Mapping[str, Tool] = REGISTRY | |
| ) -> FastAPI: | |
| settings = settings or Settings() | |
| unknown = sorted(set(settings.tool_timeouts) - set(tools_by_id)) | |
| if unknown: | |
| raise ValueError(f"AA_TOOL_TIMEOUTS names tools that do not exist: {unknown}") | |
| configure_logging() | |
| store = InMemoryJobStore( | |
| settings.jobs_root, | |
| ttl=timedelta(seconds=settings.job_ttl_seconds), | |
| max_jobs=settings.max_jobs, | |
| max_file_bytes=settings.max_file_bytes, | |
| ) | |
| runner = JobRunner(store, tools_by_id, settings) | |
| sweeper = Sweeper(store, settings.sweep_interval_seconds) | |
| def stop() -> None: | |
| sweeper.stop() | |
| runner.shutdown() | |
| store.close() | |
| async def lifespan(app: FastAPI) -> AsyncIterator[None]: | |
| sweeper.start() | |
| try: | |
| yield | |
| finally: | |
| await asyncio.to_thread(stop) | |
| app = FastAPI( | |
| title="Audit Automation", | |
| lifespan=lifespan, | |
| generate_unique_id_function=lambda route: route.name, | |
| ) | |
| app.state.settings = settings | |
| app.state.store = store | |
| app.state.runner = runner | |
| app.state.tools = tools_by_id | |
| errors.install(app) | |
| # The last middleware added runs first, so CORS headers reach every error response. | |
| app.add_middleware(BodyLimit, upload_bytes=settings.max_request_bytes) | |
| app.add_middleware(errors.CatchAll) | |
| app.add_middleware(OriginGuard, allowed=settings.frontend_origin) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=[settings.frontend_origin], | |
| allow_methods=["GET", "POST", "DELETE"], | |
| allow_headers=["Content-Type", "Authorization"], | |
| expose_headers=["Content-Disposition"], | |
| ) | |
| app.include_router(health.router) | |
| app.include_router(tools.router(tools_by_id, settings)) | |
| app.include_router(jobs.router(store, runner, tools_by_id, settings)) | |
| return app | |