Spaces:
Sleeping
Sleeping
Download backend/main.py from solvorlabs/docforge-api: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/spaces/solvorlabs/docforge-api/resolve/main/backend/main.py
- Command line
-
hf download hf://spaces/solvorlabs/docforge-api/backend/main.py
-
curl -L -o main.py https://huggingface.co/spaces/solvorlabs/docforge-api/resolve/main/backend/main.py
3.58 kB
| """ | |
| DocForge FastAPI Backend | |
| Thin client / fat backend architecture: | |
| - All heavy lifting (crawling, structuring, caching) happens here | |
| - VS Code extension and MCP server are thin HTTP clients to this API | |
| Run locally: | |
| uvicorn backend.main:app --reload --port 8000 | |
| Or via Makefile: | |
| make dev-backend | |
| """ | |
| import logging | |
| import os | |
| import time | |
| from dotenv import load_dotenv | |
| from fastapi import FastAPI, Request | |
| load_dotenv() | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| from .routers import auth, context, corrections, search, versions, oauth | |
| # ββ Logging setup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # ββ App initialization ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI( | |
| title="DocForge API", | |
| description=( | |
| "Turn any library's documentation into a prompt-ready .context.md file, " | |
| "pinned to the exact version you're using." | |
| ), | |
| version="1.0.0", | |
| docs_url="/docs", | |
| redoc_url="/redoc", | |
| ) | |
| # Allow VS Code extension and MCP server to call from any origin during dev | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # ββ Request timing middleware βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def log_request_time(request: Request, call_next): | |
| t0 = time.time() | |
| response = await call_next(request) | |
| elapsed_ms = (time.time() - t0) * 1000 | |
| logger.info("%s %s β %d (%.0fms)", request.method, request.url.path, response.status_code, elapsed_ms) | |
| return response | |
| # ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app.include_router(auth.router) | |
| app.include_router(oauth.router) | |
| app.include_router(context.router) | |
| app.include_router(corrections.router) | |
| app.include_router(search.router) | |
| app.include_router(versions.router) | |
| async def health_check() -> dict: | |
| """Health check endpoint β used by VS Code extension to verify backend connectivity.""" | |
| return { | |
| "status": "ok", | |
| "service": "docforge-api", | |
| "version": "1.0.0", | |
| "dev_mode": os.getenv("DEV_MODE", "true").lower() == "true", | |
| } | |
| async def root() -> dict: | |
| return { | |
| "message": "DocForge API is running", | |
| "docs": "/docs", | |
| "health": "/api/health", | |
| } | |
| # ββ Global error handler ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def global_exception_handler(request: Request, exc: Exception) -> JSONResponse: | |
| logger.exception("Unhandled exception on %s: %s", request.url, exc) | |
| return JSONResponse( | |
| status_code=500, | |
| content={"detail": f"Internal server error: {str(exc)}"}, | |
| ) |