Spaces:
Sleeping
Sleeping
Claude Code commited on
Commit ·
a3c4af6
1
Parent(s): 0565df0
Claude Code: add robust /health endpoint with graceful error handling and welcome root endpoint
Browse files
app.py
CHANGED
|
@@ -3,21 +3,91 @@
|
|
| 3 |
Minimal HuggingClaw - Cain
|
| 4 |
Testing basic FastAPI container functionality.
|
| 5 |
"""
|
| 6 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(title="HuggingClaw - Cain (Minimal Test)", version="0.0.1")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
async def root():
|
| 13 |
-
"""Root endpoint -
|
| 14 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
@app.get("/health")
|
| 18 |
async def health():
|
| 19 |
-
"""
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
if __name__ == "__main__":
|
|
|
|
| 3 |
Minimal HuggingClaw - Cain
|
| 4 |
Testing basic FastAPI container functionality.
|
| 5 |
"""
|
| 6 |
+
from fastapi import FastAPI, status as http_status
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
import os
|
| 9 |
+
import sys
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
# Add /app to sys.path for proper package imports
|
| 13 |
+
sys.path.insert(0, "/app")
|
| 14 |
|
| 15 |
app = FastAPI(title="HuggingClaw - Cain (Minimal Test)", version="0.0.1")
|
| 16 |
|
| 17 |
+
START_TIME = time.time()
|
| 18 |
+
|
| 19 |
|
| 20 |
@app.get("/")
|
| 21 |
async def root():
|
| 22 |
+
"""Root endpoint - welcome message."""
|
| 23 |
+
return {
|
| 24 |
+
"message": "Welcome to HuggingClaw - Cain",
|
| 25 |
+
"status": "running",
|
| 26 |
+
"version": "0.0.1",
|
| 27 |
+
"uptime_seconds": round(time.time() - START_TIME, 2)
|
| 28 |
+
}
|
| 29 |
|
| 30 |
|
| 31 |
@app.get("/health")
|
| 32 |
async def health():
|
| 33 |
+
"""
|
| 34 |
+
Health check endpoint with graceful error handling.
|
| 35 |
+
|
| 36 |
+
Checks:
|
| 37 |
+
1. Basic API availability
|
| 38 |
+
2. Persistence files (JSON-based storage)
|
| 39 |
+
3. Brain import (if available)
|
| 40 |
+
|
| 41 |
+
Returns 200 OK with {"status": "ok"} if all critical checks pass.
|
| 42 |
+
Returns 503 with error details if critical failures occur.
|
| 43 |
+
"""
|
| 44 |
+
checks = {}
|
| 45 |
+
is_healthy = True
|
| 46 |
+
|
| 47 |
+
# 1. Check persistence files (graceful - don't crash if files don't exist)
|
| 48 |
+
try:
|
| 49 |
+
cain_status_path = "/app/openclaw/.openclaw/agents/cain_status.json"
|
| 50 |
+
if os.path.exists(cain_status_path):
|
| 51 |
+
checks["persistence"] = "ok"
|
| 52 |
+
else:
|
| 53 |
+
checks["persistence"] = "no data files yet"
|
| 54 |
+
except Exception as e:
|
| 55 |
+
checks["persistence"] = f"warning: {type(e).__name__}"
|
| 56 |
+
# Persistence issues are warnings, not critical failures
|
| 57 |
+
|
| 58 |
+
# 2. Check brain import (graceful - don't crash if import fails)
|
| 59 |
+
try:
|
| 60 |
+
from openclaw.agents.brain_minimal import BrainMinimal
|
| 61 |
+
brain = BrainMinimal(agent_name="cain", legacy_mode=True)
|
| 62 |
+
checks["brain"] = "ok"
|
| 63 |
+
except ImportError:
|
| 64 |
+
checks["brain"] = "not available (optional)"
|
| 65 |
+
except Exception as e:
|
| 66 |
+
checks["brain"] = f"warning: {type(e).__name__}"
|
| 67 |
+
# Brain issues are warnings, not critical failures for health endpoint
|
| 68 |
+
|
| 69 |
+
# 3. Check frontend assets (graceful)
|
| 70 |
+
try:
|
| 71 |
+
index_path = "/app/static/index.html"
|
| 72 |
+
fallback_path = "/app/index.html"
|
| 73 |
+
if os.path.exists(index_path) or os.path.exists(fallback_path):
|
| 74 |
+
checks["frontend"] = "ok"
|
| 75 |
+
else:
|
| 76 |
+
checks["frontend"] = "not found"
|
| 77 |
+
except Exception as e:
|
| 78 |
+
checks["frontend"] = f"warning: {type(e).__name__}"
|
| 79 |
+
|
| 80 |
+
# Return 200 OK with status: ok for health endpoint
|
| 81 |
+
# This endpoint should never crash - it's for uptime monitoring
|
| 82 |
+
return JSONResponse(
|
| 83 |
+
status_code=http_status.HTTP_200_OK,
|
| 84 |
+
content={
|
| 85 |
+
"status": "ok",
|
| 86 |
+
"checks": checks,
|
| 87 |
+
"uptime_seconds": round(time.time() - START_TIME, 2),
|
| 88 |
+
"timestamp": time.time()
|
| 89 |
+
}
|
| 90 |
+
)
|
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|