Serve individual documentation files.
(filename: str)
| 138 | |
| 139 | @app.get("/{filename:path}", response_class=HTMLResponse) |
| 140 | async def serve_doc(filename: str): |
| 141 | """Serve individual documentation files.""" |
| 142 | initialize_globals() |
| 143 | |
| 144 | if DOCS_FOLDER is None: |
| 145 | raise HTTPException(status_code=500, detail="Documentation folder not configured. Please set DOCS_FOLDER environment variable or run with --docs-folder argument.") |
| 146 | |
| 147 | # Security check: ensure we're only serving .md files and they exist in the docs folder |
| 148 | if not filename.endswith('.md'): |
| 149 | raise HTTPException(status_code=404, detail="Only markdown files are supported") |
| 150 | |
| 151 | file_path = Path(DOCS_FOLDER) / filename |
| 152 | |
| 153 | # Ensure the file is within the docs folder (prevent directory traversal) |
| 154 | try: |
| 155 | file_path = file_path.resolve() |
| 156 | docs_folder_resolved = Path(DOCS_FOLDER).resolve() |
| 157 | if not str(file_path).startswith(str(docs_folder_resolved)): |
| 158 | raise HTTPException(status_code=403, detail="Access denied") |
| 159 | except Exception: |
| 160 | raise HTTPException(status_code=403, detail="Invalid file path") |
| 161 | |
| 162 | if not file_path.exists(): |
| 163 | raise HTTPException(status_code=404, detail=f"File {filename} not found") |
| 164 | |
| 165 | try: |
| 166 | content = file_manager.load_text(file_path) |
| 167 | |
| 168 | html_content = markdown_to_html(content) |
| 169 | title = get_file_title(file_path) |
| 170 | |
| 171 | context = { |
| 172 | "title": title, |
| 173 | "content": html_content, |
| 174 | "navigation": MODULE_TREE, |
| 175 | "current_page": filename |
| 176 | } |
| 177 | |
| 178 | return HTMLResponse(content=render_template(DOCS_VIEW_TEMPLATE, context)) |
| 179 | |
| 180 | except Exception as e: |
| 181 | raise HTTPException(status_code=500, detail=f"Error reading {filename}: {e}") |
| 182 | |
| 183 | |
| 184 | # Mount static files |
nothing calls this directly
no test coverage detected