MARKER
!/usr/bin/env python3¶
"""Patch COG API to serve HTML dashboard at root""" import os
API_PATH = "/var/www/smarthub.my/cog/api.py"
Read current api.py¶
with open(API_PATH) as f: content = f.read()
Check if already patched¶
if "text/html" in content: print("Already patched!") else: # Add HTML response import and dashboard endpoint patch = ''' from fastapi.responses import HTMLResponse
DASHBOARD_HTML = """<!DOCTYPE html>
COGLIVE
Balances
| Agent | COG | Share |
|---|
""" '''
# Insert before the app = FastAPI line
content = content.replace(
'app = FastAPI(',
patch + '\napp = FastAPI('
)
# Replace the root endpoint to serve HTML
content = content.replace(
'''@app.get("/")
def root(): return {"name": "COG", "version": "1.0.0", "network": "CIPHER BuildNet", "status": "live"}''', '''@app.get("/", response_class=HTMLResponse) def root(): return DASHBOARD_HTML
@app.get("/api") def api_root(): return {"name": "COG", "version": "1.0.0", "network": "CIPHER BuildNet", "status": "live"}''' )
with open(API_PATH, 'w') as f:
f.write(content)
print("✓ API patched with HTML dashboard at /cog/")
Restart uvicorn¶
import subprocess subprocess.run(["pkill", "-f", "uvicorn api:app.*8420"], capture_output=True) import time; time.sleep(2) subprocess.Popen( ["python3", "-m", "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8420", "--workers", "2"], cwd="/var/www/smarthub.my/cog", stdout=open("/var/log/cog.log", "w"), stderr=subprocess.STDOUT ) time.sleep(3)
Test¶
import urllib.request, json try: r = urllib.request.urlopen("http://localhost:8420/stats", timeout=5) d = json.loads(r.read()) print(f"✓ API restarted — Supply: {d['total_supply_cog']:,.0f} COG") except Exception as e: print(f"⚠ Restart may need manual: {e}")