forked from retoor/devplacepy
The diff adds the mandatory `retoor <retoor@molodetz.nl>` header comment to every `.py` file under `devplacepy/` (including `__init__.py` and `routers/__init__.py`), and updates the `StyleAgent` in `agents/style.py` to instruct agents to only add the header on created or already-edited files rather than sweeping the entire repo. The `agents/base.py` operating protocol is also revised to reorder and clarify tool discipline rules.
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import hashlib
|
|
import logging
|
|
from fastapi import APIRouter, Request
|
|
from fastapi.responses import Response
|
|
from devplacepy.avatar import generate_avatar_svg
|
|
from devplacepy.cache import TTLCache
|
|
from devplacepy.config import SECONDS_PER_DAY
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
_cache = TTLCache(ttl=SECONDS_PER_DAY, max_size=4096)
|
|
_CACHE_CONTROL = f"public, max-age={SECONDS_PER_DAY}, immutable"
|
|
|
|
|
|
@router.get("/{style}/{seed}")
|
|
async def avatar_proxy(request: Request, style: str, seed: str, size: int = 128):
|
|
cache_key = f"{seed}:{size}"
|
|
etag = '"' + hashlib.md5(cache_key.encode("utf-8")).hexdigest() + '"'
|
|
headers = {"ETag": etag, "Cache-Control": _CACHE_CONTROL}
|
|
|
|
if request.headers.get("if-none-match") == etag:
|
|
return Response(status_code=304, headers=headers)
|
|
|
|
svg = _cache.get(cache_key)
|
|
if svg is None:
|
|
svg = generate_avatar_svg(seed)
|
|
_cache.set(cache_key, svg)
|
|
return Response(content=svg, media_type="image/svg+xml", headers=headers)
|