Spaces:
Sleeping
Sleeping
File size: 3,489 Bytes
8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd 8456fc7 ae4c5bd | 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 77 78 79 80 81 82 83 84 85 86 87 | """
์ค์บ ์ฌ์ฉ๋ ์กฐํ(GET)์ ์ง๊ณ ์ฆ๊ฐ(POST).
๋ ๋ฉ์๋๊ฐ ๊ฐ์ ํ
์ด๋ธ(`usage_daily.scans`)์ ๋ณธ๋ค. ์์ ์๋ GET์ด `usage_daily`๋ฅผ ์ฝ๊ณ
POST๊ฐ `daily_usage`์ ์จ์ ์ฌ๋ฆฐ ๊ฐ์ ์๊ธฐ๊ฐ ์ฝ์ง ๋ชปํ๋ค. GET์ด ๋๋ ค์ฃผ๋ `today`๋ ์ค์บ์ด
์๋๋ผ ๋ถ์ ์(analyses)์๋ค. ์ง๊ธ์ ๋ ๋ค `usage_daily.scans` ํ๋๋ง ๋ณธ๋ค.
**ํ๋๋ ์๋ค.** ์์ ์ ์ค์บ 10ํ/์ผ์ GPU ๋น์ฉ ๋ฐฉ์ด ์ฅ์น์๋๋ฐ ๋ฌด๋ฃ Space ์ฝ๋์คํํธ๋ก
ํธ์ถ์ด ์คํจํด๋ ํ ๋น๋์ด ๊น์ฌ, ์ฌ์ฉ์๊ฐ ํ๋๋ฅผ ์์ ๊ธฐ๋ก ํ๋ค. ๊ทธ๋์ ์ด๊ณผ ๊ฑฐ์ ์๋ต๋,
`limit`ยท`remaining` ํ๋๋ ์ฌ๋ผ์ก๋ค. ์ซ์๋ฅผ ๊ณ์ ์ธ๋ ์ด์ ๋ ๋น์ฉ์ด ํ์์ ๋ ๋๊ฐ ์ผ๋ง๋
์ผ๋์ง ์ถ์ ํ๊ธฐ ์ํจ์ด๋ฉฐ, ๊ฐ์ธ์ ๋ณด์ฒ๋ฆฌ๋ฐฉ์นจ์ ์์ง ๋ชฉ์ ("ํต๊ณยท๋จ์ฉ ๋ฐฉ์ง")๊ณผ ์ผ์นํ๋ค.
"""
import os
from typing import Any
import supabase as sb
from fastapi import APIRouter, Header, HTTPException
from middleware.rate_limit import get_usage_daily, increment_scan_daily
router = APIRouter()
SUPABASE_URL = os.environ["SUPABASE_URL"]
SUPABASE_SERVICE_ROLE_KEY = os.environ["SUPABASE_SERVICE_ROLE_KEY"]
def get_supabase() -> sb.Client:
return sb.create_client(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY)
def extract_user_id(token: str, db: sb.Client) -> str:
try:
user_response = db.auth.get_user(token)
return str(user_response.user.id)
except Exception as e:
raise HTTPException(status_code=401, detail=f"์ ํจํ์ง ์์ ํ ํฐ์
๋๋ค: {e}")
def _parse_bearer(authorization: str) -> str:
if not authorization.startswith("Bearer "):
raise HTTPException(status_code=401, detail="Authorization ํค๋๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.")
return authorization.removeprefix("Bearer ").strip()
def _total_scans(user_id: str, db: sb.Client) -> int:
result = db.table("usage_daily").select("scans").eq("user_id", user_id).execute()
return sum(int(row.get("scans") or 0) for row in (result.data or []))
@router.get("/scan-usage")
async def get_scan_usage(authorization: str = Header(...)) -> dict[str, Any]:
"""์ค๋ ์ค์บ ์ฌ์ฉ ํํฉ. `today`๋ **์ค๋ ์ค์บ ์**, `total`์ ๋์ ์ค์บ ์๋ค."""
token = _parse_bearer(authorization)
db = get_supabase()
user_id = extract_user_id(token, db)
today_scans = get_usage_daily(user_id, db)["scans"]
return {
"today": today_scans,
"total": _total_scans(user_id, db),
}
@router.post("/scan-usage")
async def claim_scan_usage(authorization: str = Header(...)) -> dict[str, Any]:
"""
์ค์บ 1ํ๋ฅผ ์ง๊ณ์ ๋ํ๋ค. ํ๋๊ฐ ์์ผ๋ฏ๋ก ์ด๋ค ๊ฒฝ์ฐ์๋ ๊ฑฐ์ ํ์ง ์๋๋ค.
์ฑ์ ์ธ์ ์๋ฒ๋ฅผ ๋ถ๋ฅด๊ธฐ **์ ์** ์ด๊ฑธ ๋ถ๋ฅธ๋ค. ์ธ์ ์๋ฒ๋ ์ฑ์ด ์ง์ (๋ฐฑ์๋๋ฅผ ๊ฑฐ์น์ง ์๊ณ )
๋ถ๋ฅด๋ฏ๋ก ์๋ฒ๊ฐ ํธ์ถ ์๋ฅผ ์ ์ ์๋ ์ง์ ์ด ์ฌ๊ธฐ๋ฟ์ด๋ค. ๋ค๋ฆ๊ฒ ์ธ๋ฉด ์คํจ๋ก ๋น ์ ธ๋๊ฐ
ํธ์ถ์ด ์ง๊ณ์์ ๋๋ฝ๋๋ค.
์นด์ดํฐ๋ฅผ service_role๋ก๋ง ์ฐ๋ ์ด์ : ํด๋ผ์ด์ธํธ์ `usage_daily` ์ฐ๊ธฐ ๊ถํ์ ์ฃผ๋ฉด ์๊ธฐ
์ฌ์ฉ๋์ 0์ผ๋ก ๋๋๋ฆด ์ ์์ด ์ถ์ ๊ธฐ๋ก์ด ๋ฏฟ์ ์ ์๊ฒ ๋๋ค.
"""
token = _parse_bearer(authorization)
db = get_supabase()
user_id = extract_user_id(token, db)
new_scans = increment_scan_daily(user_id, db)
return {
"today": new_scans,
"total": _total_scans(user_id, db),
}
|