File size: 2,616 Bytes
29bfc1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import asyncio
import time
from datetime import datetime, timezone

from fastapi import APIRouter, Form, HTTPException, Request, Depends

from src.core.config import DEFAULT_PINECONE_KEY
from src.core.security import get_verified_keys
from src.services.db_client import cld_ping, ensure_indexes, pinecone_pool
from src.core.logging import log
from src.common.utils import get_ip, is_default_key

router = APIRouter()

@router.get("/")
async def root():
    return {"status": "ok"}

@router.get("/api/health")
async def health():
    return {"status": "ok", "timestamp": datetime.now(timezone.utc).isoformat()}

@router.post("/api/log")
async def frontend_log(
    request:  Request,
    event:    str = Form(...),
    user_id:  str = Form(""),
    page:     str = Form(""),
    metadata: str = Form("{}"),
):
    import json
    ip = get_ip(request)
    try:
        meta = json.loads(metadata) if metadata else {}
    except Exception:
        meta = {}
    log(
        "INFO", f"frontend.{event}",
        user_id=user_id or "anonymous",
        page=page, ip=ip,
        ua=request.headers.get("User-Agent", "")[:120],
        **meta,
    )
    return {"ok": True}

@router.post("/api/verify-keys")
async def verify_keys(
    request: Request,
    user_id: str = Form(""),
    keys: dict = Depends(get_verified_keys)
):
    ip    = get_ip(request)
    mode  = "guest" if is_default_key(keys["pinecone_key"], DEFAULT_PINECONE_KEY) else "personal"
    start = time.perf_counter()
    log("INFO", "settings.verify_keys.start", user_id=user_id or "anonymous", mode=mode, ip=ip)

    try:
        await asyncio.to_thread(cld_ping, keys["cloudinary_creds"])
    except Exception as e:
        log("ERROR", "settings.verify_keys.cloudinary_fail", user_id=user_id or "anonymous", ip=ip, error=str(e))
        raise HTTPException(400, "Invalid Cloudinary Environment URL.")

    indexes_created: list[str] = []
    try:
        pc = pinecone_pool.get(keys["pinecone_key"])
        indexes_created = await asyncio.to_thread(ensure_indexes, pc)
    except Exception as e:
        err = str(e)
        clean = "Invalid Pinecone API Key." if "401" in err or "unauthorized" in err.lower() else f"Pinecone Error: {err}"
        log("ERROR", "settings.verify_keys.pinecone_fail", user_id=user_id or "anonymous", ip=ip, error=clean)
        raise HTTPException(400, clean)

    log("INFO", "settings.verify_keys.success", user_id=user_id or "anonymous", mode=mode, ip=ip,
        indexes_created=indexes_created, duration_ms=round((time.perf_counter() - start) * 1000))
    return {"message": "Keys verified and indexes ready!"}