MuscleCare-FastAPI / start.py
Merry99's picture
add local test
1c094f3
raw
history blame
2.27 kB
import os, base64, zipfile, io, sys, tempfile, subprocess
from pathlib import Path
from dotenv import load_dotenv
# .env 파일 λ‘œλ“œ (둜컬 개발용)
load_dotenv()
def prepare_wallet_dir():
"""
Wallet μ€€λΉ„:
- 배포 ν™˜κ²½: WALLET_ZIP_B64μ—μ„œ λ””μ½”λ”©
- 둜컬 ν™˜κ²½: WALLET_DIR 경둜 μ‚¬μš©
"""
# 둜컬 개발: WALLET_DIR이 이미 μ„€μ •λ˜μ–΄ μžˆλŠ” 경우
if "WALLET_DIR" in os.environ:
wallet_dir = os.environ["WALLET_DIR"]
if Path(wallet_dir).exists():
print(f"πŸ“ 둜컬 Wallet μ‚¬μš©: {wallet_dir}")
return wallet_dir
# 배포 ν™˜κ²½: WALLET_ZIP_B64μ—μ„œ λ””μ½”λ”©
if "WALLET_ZIP_B64" not in os.environ:
raise RuntimeError(
"❌ WALLET_ZIP_B64 λ˜λŠ” WALLET_DIR ν™˜κ²½ λ³€μˆ˜κ°€ ν•„μš”ν•©λ‹ˆλ‹€.\n"
" 둜컬: .env νŒŒμΌμ— WALLET_DIR μ„€μ •\n"
" 배포: Hugging Face Secrets에 WALLET_ZIP_B64 μ„€μ •"
)
print("πŸ” Wallet Base64 λ””μ½”λ”© μ‹œμž‘...")
b64 = os.environ["WALLET_ZIP_B64"]
wallet_password = os.environ["WALLET_PASSWORD"]
# wallet을 λ©”λͺ¨λ¦¬->μž„μ‹œλ””λ ‰ν† λ¦¬λ‘œ 볡원
wallet_dir = tempfile.mkdtemp(prefix="wallet_")
print(f"πŸ“ μž„μ‹œ 디렉토리: {wallet_dir}")
zip_bytes = base64.b64decode(b64)
with zipfile.ZipFile(io.BytesIO(zip_bytes)) as z:
z.extractall(wallet_dir)
# μ•ˆμ „μ„ μœ„ν•΄ κΆŒν•œ μΆ•μ†Œ
for root, _, files in os.walk(wallet_dir):
for f in files:
os.chmod(os.path.join(root, f), 0o600)
print("βœ… Wallet μ••μΆ• ν•΄μ œ μ™„λ£Œ")
# μ§€κ°‘ λΉ„λ°€λ²ˆν˜ΈλŠ” oracledbμ—μ„œ μ‚¬μš©
os.environ["WALLET_DIR"] = wallet_dir
os.environ["WALLET_PASSWORD"] = wallet_password
return wallet_dir
if __name__ == "__main__":
print("=" * 60)
print("πŸš€ MuscleCare FastAPI μ„œλ²„ μ‹œμž‘")
print("=" * 60)
# Wallet μ€€λΉ„
try:
prepare_wallet_dir()
except Exception as e:
print(f"❌ Wallet μ€€λΉ„ μ‹€νŒ¨: {e}")
sys.exit(1)
# Uvicorn 기동
print("\n🌐 FastAPI μ„œλ²„ μ‹€ν–‰...")
print("=" * 60)
subprocess.run([sys.executable, "-m", "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"])