Check / app.py
Fred808's picture
Update app.py
115ce0e verified
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
import shutil
import uvicorn
app = FastAPI(title="Disk Space Monitor", description="A simple API to monitor disk space")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_disk_space():
"""Get disk space information"""
total, used, free = shutil.disk_usage("/")
return {
"total_gb": round(total / (1024**3), 2),
"used_gb": round(used / (1024**3), 2),
"free_gb": round(free / (1024**3), 2),
"used_percentage": round((used / total) * 100, 2)
}
@app.get("/")
async def root():
"""Root endpoint with HTML interface"""
disk_info = get_disk_space()
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>Disk Space Monitor</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 40px; background-color: #f5f5f5; }}
.container {{ max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
h1 {{ color: #333; text-align: center; }}
.disk-info {{ background: #f8f9fa; padding: 20px; border-radius: 8px; margin: 20px 0; }}
.metric {{ display: flex; justify-content: space-between; margin: 10px 0; padding: 10px; background: white; border-radius: 5px; }}
.metric-label {{ font-weight: bold; color: #555; }}
.metric-value {{ color: #007bff; font-weight: bold; }}
.progress-bar {{ width: 100%; height: 20px; background: #e9ecef; border-radius: 10px; overflow: hidden; margin: 10px 0; }}
.progress-fill {{ height: 100%; background: linear-gradient(90deg, #28a745, #ffc107, #dc3545); transition: width 0.3s ease; }}
.refresh-btn {{ background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; margin: 10px 0; }}
.refresh-btn:hover {{ background: #0056b3; }}
</style>
</head>
<body>
<div class="container">
<h1>🖥️ Disk Space Monitor</h1>
<div class="disk-info">
<div class="metric">
<span class="metric-label">Total Space:</span>
<span class="metric-value">{disk_info['total_gb']} GB</span>
</div>
<div class="metric">
<span class="metric-label">Used Space:</span>
<span class="metric-value">{disk_info['used_gb']} GB</span>
</div>
<div class="metric">
<span class="metric-label">Free Space:</span>
<span class="metric-value">{disk_info['free_gb']} GB</span>
</div>
<div class="metric">
<span class="metric-label">Usage:</span>
<span class="metric-value">{disk_info['used_percentage']}%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" style="width: {disk_info['used_percentage']}%"></div>
</div>
<button class="refresh-btn" onclick="location.reload()">🔄 Refresh</button>
</div>
<p style="text-align: center; color: #666; margin-top: 30px;">
API Endpoints: <a href="/api/disk-space">/api/disk-space</a> | <a href="/docs">/docs</a>
</p>
</div>
</body>
</html>
"""
return HTMLResponse(content=html_content)
@app.get("/api/disk-space")
async def get_disk_space_api():
"""API endpoint to get disk space information in JSON format"""
return get_disk_space()
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "service": "disk-space-monitor"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)