-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseo.py
More file actions
50 lines (38 loc) · 1.51 KB
/
seo.py
File metadata and controls
50 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""SEO endpoints (sitemap)."""
import html
from fastapi import APIRouter, Depends
from fastapi.responses import Response
from sqlalchemy.ext.asyncio import AsyncSession
from api.cache import cache_key, get_cache, set_cache
from api.dependencies import optional_db
from core.database import SpecRepository
router = APIRouter(tags=["seo"])
@router.get("/sitemap.xml")
async def get_sitemap(db: AsyncSession | None = Depends(optional_db)):
"""
Generate dynamic XML sitemap for SEO.
Includes root, catalog page, and all specs with implementations.
"""
key = cache_key("sitemap_xml")
cached = get_cache(key)
if cached:
return Response(content=cached, media_type="application/xml")
# Build XML lines
xml_lines = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
" <url><loc>https://pyplots.ai/</loc></url>",
" <url><loc>https://pyplots.ai/catalog</loc></url>",
]
# Add spec URLs (only specs with implementations)
if db is not None:
repo = SpecRepository(db)
specs = await repo.get_all()
for spec in specs:
if spec.impls: # Only include specs with implementations
spec_id = html.escape(spec.id)
xml_lines.append(f" <url><loc>https://pyplots.ai/{spec_id}</loc></url>")
xml_lines.append("</urlset>")
xml = "\n".join(xml_lines)
set_cache(key, xml)
return Response(content=xml, media_type="application/xml")