|
|
""" |
|
|
KSTools License Manager - 授權系統資料庫連接 |
|
|
專門處理授權系統 (Project 1) 的資料庫操作 |
|
|
""" |
|
|
|
|
|
import os |
|
|
from supabase import create_client, Client |
|
|
from typing import Optional |
|
|
|
|
|
class LicenseDatabase: |
|
|
"""授權系統 Supabase 資料庫客戶端""" |
|
|
|
|
|
def __init__(self): |
|
|
|
|
|
self.url: str = os.environ.get("SUPABASE_LICENSE_URL", "") |
|
|
self.key: str = os.environ.get("SUPABASE_LICENSE_SERVICE_KEY", "") |
|
|
self.client: Optional[Client] = None |
|
|
|
|
|
print(f"🔗 Initializing License System Supabase connection...") |
|
|
print(f" URL: {self.url[:50]}{'...' if len(self.url) > 50 else ''}") |
|
|
print(f" Key: {'*' * 20 if self.key else 'NOT SET'}") |
|
|
|
|
|
if self.url and self.key and not self.url.startswith('your-') and not self.key.startswith('your-'): |
|
|
try: |
|
|
self.client = create_client(self.url, self.key) |
|
|
print("✅ License System Supabase connection initialized successfully") |
|
|
except Exception as e: |
|
|
print(f"❌ License System Supabase connection failed: {e}") |
|
|
self.client = None |
|
|
else: |
|
|
print("⚠️ License System Supabase 環境變數未設定或使用預設值") |
|
|
print(" 請在 Hugging Face Spaces Settings 或 .env 檔案中設定正確的 SUPABASE_LICENSE_URL 和 SUPABASE_LICENSE_SERVICE_KEY") |
|
|
|
|
|
def get_client(self) -> Optional[Client]: |
|
|
"""取得授權系統 Supabase 客戶端""" |
|
|
if not self.client: |
|
|
print("❌ License System Supabase client 未初始化") |
|
|
return self.client |
|
|
|
|
|
def is_connected(self) -> bool: |
|
|
"""檢查授權系統是否連接成功""" |
|
|
connected = self.client is not None |
|
|
print(f"🔍 License System Supabase connection status: {'Connected' if connected else 'Not connected'}") |
|
|
return connected |
|
|
|
|
|
|
|
|
license_db = LicenseDatabase() |
|
|
|
|
|
|
|
|
db = license_db |