You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
732 B
Python

"""Docs router — serves markdown files from the docs/ directory."""
from __future__ import annotations
from pathlib import Path
from fastapi import APIRouter, HTTPException
router = APIRouter(prefix="/docs", tags=["docs"])
_DOCS_DIR = Path("docs")
@router.get("/{filename}")
async def get_doc(filename: str) -> dict:
# Prevent path traversal — strip to bare filename only
safe_name = Path(filename).name
if not safe_name.endswith(".md"):
safe_name += ".md"
path = _DOCS_DIR / safe_name
if not path.exists() or not path.is_file():
raise HTTPException(status_code=404, detail=f"Doc '{safe_name}' not found")
return {"content": path.read_text(encoding="utf-8"), "filename": safe_name}