Spaces:
Sleeping
Sleeping
Create system.py
Browse files- src/api/system.py +75 -0
src/api/system.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import time
|
| 3 |
+
from datetime import datetime, timezone
|
| 4 |
+
|
| 5 |
+
from fastapi import APIRouter, Form, HTTPException, Request, Depends
|
| 6 |
+
|
| 7 |
+
from src.core.config import DEFAULT_PINECONE_KEY
|
| 8 |
+
from src.core.security import get_verified_keys
|
| 9 |
+
from src.services.db_client import cld_ping, ensure_indexes, pinecone_pool
|
| 10 |
+
from src.core.logging import log
|
| 11 |
+
from src.common.utils import get_ip, is_default_key
|
| 12 |
+
|
| 13 |
+
router = APIRouter()
|
| 14 |
+
|
| 15 |
+
@router.get("/")
|
| 16 |
+
async def root():
|
| 17 |
+
return {"status": "ok"}
|
| 18 |
+
|
| 19 |
+
@router.get("/api/health")
|
| 20 |
+
async def health():
|
| 21 |
+
return {"status": "ok", "timestamp": datetime.now(timezone.utc).isoformat()}
|
| 22 |
+
|
| 23 |
+
@router.post("/api/log")
|
| 24 |
+
async def frontend_log(
|
| 25 |
+
request: Request,
|
| 26 |
+
event: str = Form(...),
|
| 27 |
+
user_id: str = Form(""),
|
| 28 |
+
page: str = Form(""),
|
| 29 |
+
metadata: str = Form("{}"),
|
| 30 |
+
):
|
| 31 |
+
import json
|
| 32 |
+
ip = get_ip(request)
|
| 33 |
+
try:
|
| 34 |
+
meta = json.loads(metadata) if metadata else {}
|
| 35 |
+
except Exception:
|
| 36 |
+
meta = {}
|
| 37 |
+
log(
|
| 38 |
+
"INFO", f"frontend.{event}",
|
| 39 |
+
user_id=user_id or "anonymous",
|
| 40 |
+
page=page, ip=ip,
|
| 41 |
+
ua=request.headers.get("User-Agent", "")[:120],
|
| 42 |
+
**meta,
|
| 43 |
+
)
|
| 44 |
+
return {"ok": True}
|
| 45 |
+
|
| 46 |
+
@router.post("/api/verify-keys")
|
| 47 |
+
async def verify_keys(
|
| 48 |
+
request: Request,
|
| 49 |
+
user_id: str = Form(""),
|
| 50 |
+
keys: dict = Depends(get_verified_keys)
|
| 51 |
+
):
|
| 52 |
+
ip = get_ip(request)
|
| 53 |
+
mode = "guest" if is_default_key(keys["pinecone_key"], DEFAULT_PINECONE_KEY) else "personal"
|
| 54 |
+
start = time.perf_counter()
|
| 55 |
+
log("INFO", "settings.verify_keys.start", user_id=user_id or "anonymous", mode=mode, ip=ip)
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
await asyncio.to_thread(cld_ping, keys["cloudinary_creds"])
|
| 59 |
+
except Exception as e:
|
| 60 |
+
log("ERROR", "settings.verify_keys.cloudinary_fail", user_id=user_id or "anonymous", ip=ip, error=str(e))
|
| 61 |
+
raise HTTPException(400, "Invalid Cloudinary Environment URL.")
|
| 62 |
+
|
| 63 |
+
indexes_created: list[str] = []
|
| 64 |
+
try:
|
| 65 |
+
pc = pinecone_pool.get(keys["pinecone_key"])
|
| 66 |
+
indexes_created = await asyncio.to_thread(ensure_indexes, pc)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
err = str(e)
|
| 69 |
+
clean = "Invalid Pinecone API Key." if "401" in err or "unauthorized" in err.lower() else f"Pinecone Error: {err}"
|
| 70 |
+
log("ERROR", "settings.verify_keys.pinecone_fail", user_id=user_id or "anonymous", ip=ip, error=clean)
|
| 71 |
+
raise HTTPException(400, clean)
|
| 72 |
+
|
| 73 |
+
log("INFO", "settings.verify_keys.success", user_id=user_id or "anonymous", mode=mode, ip=ip,
|
| 74 |
+
indexes_created=indexes_created, duration_ms=round((time.perf_counter() - start) * 1000))
|
| 75 |
+
return {"message": "Keys verified and indexes ready!"}
|