112 lines
3.4 KiB
Python
112 lines
3.4 KiB
Python
import os
|
|
import markdown
|
|
from fastapi import APIRouter, Request, HTTPException, Depends
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
from deps import current_user
|
|
|
|
router = APIRouter()
|
|
|
|
# Whitelist allowed files.
|
|
# We look for them in the root directory relative to 'app' parent.
|
|
# Since app is in /app inside docker, the root volume mount is usually at /app or mapped.
|
|
# Code Tour Step 828 shows 'app/main.py' path analysis.
|
|
# If workdir is /app (docker), and files are in /app (root of repo mapped to /app), then "README.md" is at "./README.md".
|
|
|
|
ALLOWED_DOCS = {
|
|
"README.md": "Start Guide (Readme)",
|
|
"QUICKSTART.md": "Schnellstart",
|
|
"HANDBUCH.md": "Benutzerhandbuch",
|
|
"CODE_TOUR.md": "Code Tour (Dev)",
|
|
"ARCHITECTURE_ROLES_GROUPS.md": "Architektur",
|
|
"DIARY_RAG_README.md": "Tagebuch & RAG Info",
|
|
"CrumbTech.md": "Technische Details",
|
|
"QDRANT_ACCESS.md": "Vektor DB Access",
|
|
"deploy_security_fixes.sh": "Security Script (Source)" # Maybe viewing scripts is cool too? Let's stick to MD for now.
|
|
}
|
|
|
|
from config import get_settings
|
|
|
|
|
|
@router.get("/docs", response_class=HTMLResponse)
|
|
async def list_docs(req: Request):
|
|
"""
|
|
List available documentation files.
|
|
"""
|
|
# Check which exist
|
|
available = []
|
|
|
|
# Use configured docs path
|
|
try:
|
|
settings = get_settings()
|
|
base_path = settings.docs_path
|
|
except:
|
|
base_path = "docs"
|
|
|
|
# Check if absolute or relative
|
|
if not os.path.isabs(base_path) and not os.path.exists(base_path):
|
|
# Fallback for docker containers if path not found relative
|
|
if os.path.exists("/docs_root"):
|
|
base_path = "/docs_root"
|
|
else:
|
|
base_path = "."
|
|
|
|
for filename, title in ALLOWED_DOCS.items():
|
|
if os.path.exists(os.path.join(base_path, filename)):
|
|
available.append({"name": title, "file": filename})
|
|
|
|
return req.app.state.render(
|
|
req,
|
|
"pages/docs_index.html",
|
|
docs=available,
|
|
page_title="Dokumentation"
|
|
)
|
|
|
|
@router.get("/docs/{filename}", response_class=HTMLResponse)
|
|
async def view_doc(req: Request, filename: str):
|
|
"""
|
|
Render a specific markdown file.
|
|
"""
|
|
if filename not in ALLOWED_DOCS:
|
|
raise HTTPException(404, "File not found or not allowed.")
|
|
|
|
base_path = "docs"
|
|
try:
|
|
settings = get_settings()
|
|
base_path = settings.docs_path
|
|
except:
|
|
pass
|
|
|
|
if not os.path.isabs(base_path) and not os.path.exists(base_path):
|
|
if os.path.exists("/docs_root"):
|
|
base_path = "/docs_root"
|
|
else:
|
|
base_path = "."
|
|
|
|
file_path = os.path.join(base_path, filename)
|
|
|
|
if not os.path.exists(file_path):
|
|
raise HTTPException(404, "File not on server.")
|
|
|
|
try:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
# Convert Markdown to HTML
|
|
# Extensions for better rendering: tables, fenced_code
|
|
html_content = markdown.markdown(
|
|
content,
|
|
extensions=['tables', 'fenced_code', 'nl2br']
|
|
)
|
|
|
|
return req.app.state.render(
|
|
req,
|
|
"pages/doc_viewer.html",
|
|
doc_title=ALLOWED_DOCS[filename],
|
|
doc_content=html_content,
|
|
filename=filename
|
|
)
|
|
|
|
except Exception as e:
|
|
raise HTTPException(500, f"Error rendering document: {e}")
|