Update src/api/admin.py
Browse files- src/api/admin.py +42 -42
src/api/admin.py
CHANGED
|
@@ -16,7 +16,7 @@ import os
|
|
| 16 |
import secrets
|
| 17 |
import secrets
|
| 18 |
import time
|
| 19 |
-
from typing import Any, Dict
|
| 20 |
|
| 21 |
from fastapi import APIRouter, HTTPException, Request, Response
|
| 22 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
@@ -655,10 +655,10 @@ async def system_status(request: Request):
|
|
| 655 |
# them into the admin dashboard. Session-protected like every admin route, and
|
| 656 |
# the Worker itself additionally requires the shared bearer token.
|
| 657 |
|
| 658 |
-
@router.get("/reports")
|
| 659 |
-
async def admin_reports(request: Request, days: int = 14):
|
| 660 |
-
_require_session(request)
|
| 661 |
-
return await _worker_get("/reports", {"days": max(1, min(days, 60))})
|
| 662 |
|
| 663 |
|
| 664 |
@router.post("/reports/{report_id}/reviewed")
|
|
@@ -678,40 +678,40 @@ async def admin_report_reviewed(report_id: int, request: Request):
|
|
| 678 |
r = await client.post(f"{worker}/reports/{int(report_id)}/reviewed", headers=headers)
|
| 679 |
return r.json()
|
| 680 |
except Exception as exc:
|
| 681 |
-
raise HTTPException(status_code=502, detail=f"Worker unreachable: {exc}")
|
| 682 |
-
|
| 683 |
-
# ββ Analytics proxies ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 684 |
-
# The dashboard fetches Worker analytics through these session-protected
|
| 685 |
-
# proxies so the Worker URL and ANALYTICS_TOKEN never reach the browser.
|
| 686 |
-
|
| 687 |
-
async def _worker_get(path: str, params: dict
|
| 688 |
-
import httpx as _hx
|
| 689 |
-
from src.config import get_settings as _gs
|
| 690 |
-
settings = _gs()
|
| 691 |
-
worker = (settings.cloudflare_worker_url or "").rstrip("/")
|
| 692 |
-
if not worker:
|
| 693 |
-
raise HTTPException(status_code=503, detail="CLOUDFLARE_WORKER_URL not configured")
|
| 694 |
-
headers = {}
|
| 695 |
-
if settings.cloudflare_worker_token:
|
| 696 |
-
headers["Authorization"] = f"Bearer {settings.cloudflare_worker_token}"
|
| 697 |
-
try:
|
| 698 |
-
async with _hx.AsyncClient(timeout=10) as client:
|
| 699 |
-
r = await client.get(f"{worker}{path}", params=params or {}, headers=headers)
|
| 700 |
-
return r.json()
|
| 701 |
-
except HTTPException:
|
| 702 |
-
raise
|
| 703 |
-
except Exception as exc:
|
| 704 |
-
raise HTTPException(status_code=502, detail=f"Worker unreachable: {exc}")
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
@router.get("/analytics")
|
| 708 |
-
async def admin_analytics(request: Request):
|
| 709 |
-
_require_session(request)
|
| 710 |
-
return await _worker_get("/analytics")
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
@router.get("/analytics/recent")
|
| 714 |
-
async def admin_analytics_recent(request: Request):
|
| 715 |
-
_require_session(request)
|
| 716 |
-
return await _worker_get("/analytics/recent")
|
| 717 |
-
|
|
|
|
| 16 |
import secrets
|
| 17 |
import secrets
|
| 18 |
import time
|
| 19 |
+
from typing import Any, Dict, Optional
|
| 20 |
|
| 21 |
from fastapi import APIRouter, HTTPException, Request, Response
|
| 22 |
from fastapi.responses import HTMLResponse, RedirectResponse
|
|
|
|
| 655 |
# them into the admin dashboard. Session-protected like every admin route, and
|
| 656 |
# the Worker itself additionally requires the shared bearer token.
|
| 657 |
|
| 658 |
+
@router.get("/reports")
|
| 659 |
+
async def admin_reports(request: Request, days: int = 14):
|
| 660 |
+
_require_session(request)
|
| 661 |
+
return await _worker_get("/reports", {"days": max(1, min(days, 60))})
|
| 662 |
|
| 663 |
|
| 664 |
@router.post("/reports/{report_id}/reviewed")
|
|
|
|
| 678 |
r = await client.post(f"{worker}/reports/{int(report_id)}/reviewed", headers=headers)
|
| 679 |
return r.json()
|
| 680 |
except Exception as exc:
|
| 681 |
+
raise HTTPException(status_code=502, detail=f"Worker unreachable: {exc}")
|
| 682 |
+
|
| 683 |
+
# ββ Analytics proxies ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 684 |
+
# The dashboard fetches Worker analytics through these session-protected
|
| 685 |
+
# proxies so the Worker URL and ANALYTICS_TOKEN never reach the browser.
|
| 686 |
+
|
| 687 |
+
async def _worker_get(path: str, params: Optional[dict] = None):
|
| 688 |
+
import httpx as _hx
|
| 689 |
+
from src.config import get_settings as _gs
|
| 690 |
+
settings = _gs()
|
| 691 |
+
worker = (settings.cloudflare_worker_url or "").rstrip("/")
|
| 692 |
+
if not worker:
|
| 693 |
+
raise HTTPException(status_code=503, detail="CLOUDFLARE_WORKER_URL not configured")
|
| 694 |
+
headers = {}
|
| 695 |
+
if settings.cloudflare_worker_token:
|
| 696 |
+
headers["Authorization"] = f"Bearer {settings.cloudflare_worker_token}"
|
| 697 |
+
try:
|
| 698 |
+
async with _hx.AsyncClient(timeout=10) as client:
|
| 699 |
+
r = await client.get(f"{worker}{path}", params=params or {}, headers=headers)
|
| 700 |
+
return r.json()
|
| 701 |
+
except HTTPException:
|
| 702 |
+
raise
|
| 703 |
+
except Exception as exc:
|
| 704 |
+
raise HTTPException(status_code=502, detail=f"Worker unreachable: {exc}")
|
| 705 |
+
|
| 706 |
+
|
| 707 |
+
@router.get("/analytics")
|
| 708 |
+
async def admin_analytics(request: Request):
|
| 709 |
+
_require_session(request)
|
| 710 |
+
return await _worker_get("/analytics")
|
| 711 |
+
|
| 712 |
+
|
| 713 |
+
@router.get("/analytics/recent")
|
| 714 |
+
async def admin_analytics_recent(request: Request):
|
| 715 |
+
_require_session(request)
|
| 716 |
+
return await _worker_get("/analytics/recent")
|
| 717 |
+
|