import json import os from datetime import datetime, timezone from pathlib import Path from fastapi import APIRouter, HTTPException, Request from fastapi.responses import HTMLResponse from pydantic import BaseModel from sqlmodel import select from ..wallet import get_balance, send_token from .db import ApiKey, create_session from .logging import get_logger logger = get_logger(__name__) admin_router = APIRouter(prefix="/admin", include_in_schema=False) class WithdrawRequest(BaseModel): amount: int def login_form() -> str: return """
{content}
Your balance = Total wallet - User balance
Save this token! It represents your withdrawn balance.
| Hashed Key | Balance (mSats) | Total Spent (mSats) | Total Requests | Refund Address | Refund Time |
|---|
Found {len(log_entries)} log entries • Searched last 7 days of logs
""" @admin_router.post("/withdraw") async def withdraw( request: Request, withdraw_request: WithdrawRequest ) -> dict[str, str]: admin_cookie = request.cookies.get("admin_password") if not admin_cookie or admin_cookie != os.getenv("ADMIN_PASSWORD"): raise HTTPException(status_code=403, detail="Unauthorized") current_balance = await get_balance("sat") if withdraw_request.amount <= 0: raise HTTPException( status_code=400, detail="Withdrawal amount must be positive" ) if withdraw_request.amount > current_balance: raise HTTPException(status_code=400, detail="Insufficient wallet balance") token = await send_token(withdraw_request.amount, "sat") return {"token": token}