diff --git a/routstr/core/admin.py b/routstr/core/admin.py index b511e449..5a547347 100644 --- a/routstr/core/admin.py +++ b/routstr/core/admin.py @@ -1,5 +1,7 @@ +import json import os from datetime import datetime, timezone +from pathlib import Path from fastapi import APIRouter, HTTPException, Request from fastapi.responses import HTMLResponse @@ -8,6 +10,9 @@ 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) @@ -21,26 +26,14 @@ def login_form() -> str:
- +{content}
Your Balance: {owner_balance} sats
-The balance is calculated by subtracting the combined user balance from the total Cashu wallet balance.
-Total Cashu Balance: {current_balance} sats
-User Balance: {total_user_balance} sats
+ +Your balance = Total wallet - User balance
+Save this token! It represents your withdrawn balance.
-| Hashed Key | @@ -383,6 +357,202 @@ async def admin(request: Request) -> str: return admin_auth() +@admin_router.get("/logs/{request_id}", response_class=HTMLResponse) +async def view_logs(request: Request, request_id: str) -> str: + admin_cookie = request.cookies.get("admin_password") + if not admin_cookie or admin_cookie != os.getenv("ADMIN_PASSWORD"): + return admin_auth() + + logger.info(f"Investigating logs for request_id: {request_id}") + + # Search for log entries with this request_id + log_entries = [] + logs_dir = Path("logs") + + if logs_dir.exists(): + # Get all log files sorted by modification time (most recent first) + log_files = sorted( + logs_dir.glob("*.log"), key=lambda x: x.stat().st_mtime, reverse=True + ) + + for log_file in log_files[:7]: # Check last 7 days of logs + try: + with open(log_file, "r") as f: + for line in f: + if request_id in line: + try: + # Parse JSON log entry + log_data = json.loads(line.strip()) + log_entries.append(log_data) + except json.JSONDecodeError: + # If not JSON, include raw line + log_entries.append({"raw": line.strip()}) + except Exception as e: + logger.error(f"Error reading log file {log_file}: {e}") + + # Sort entries by timestamp if available + log_entries.sort(key=lambda x: x.get("asctime", ""), reverse=False) + + # Format log entries for display + formatted_logs = [] + for entry in log_entries: + if "raw" in entry: + formatted_logs.append(f'
|---|